有办法用jQuery
做到这一点吗?
设置如下:
<input type='checkbox' class='sk1' /> <input type='text' class='skill1' />
<input type='checkbox' class='sk2' /> <input type='text' class='skill2' />
<input type='checkbox' class='sk3' /> <input type='text' class='skill3' />
<input type="button" onclick="validate();" />
重要提示:复选框右侧的输入字段与复选框关联
当单击按钮时,我希望进行此检查:选中的每个checkbox
的右侧都可以有一个空的input
字段。当它找到一个字段时,它将停止并抛出一个alert
。
环顾四周,我发现我可能需要使用regex
,也许还需要使用jQuery .each
。
伪代码:
for each checkbox class^=sk[number]
check if input[type=text] that has a class with skill[same number as above]
if empty, alert,
Otherwise, continue checking other boxes
(旁注:写那个伪让我想知道是否有更好的命名约定可以使用,这样我就不必从中提取特定的数字。)
(另一个注意事项:当复选框未选中时,文本字段将被禁用;我有这个功能)
有人能给我一些指导吗?
只需循环复选框并检查next
文本元素:
$(":checkbox:checked").each(function() {
if (!$(this).next(":text").val().length) {
alert("You must put in text!");
return false; //bail out of the loop, return true to skip to the next iteration
}
});
演示:http://jsfiddle.net/uvQ6C/