不仅从子函数退出,而且从整个父函数退出
简而言之
我想做的是:首先,函数 validate()
必须一一检查所有输入:如果它们不为空(或空白),则移至 if 语句(用于检查单选按钮)但是如果某些输入为空,则停止整个验证功能并专注于空输入。
这是结果: http://jsfiddle.net/tt13/y53tv/4/
只需按ok
按钮,您会看到它完成了第一个功能,并且如果也完成了,则会触发。但我想退出整个 validate() 函数,以防存在空字段,而不仅仅是从 every() 函数
退出 详细
JS
function validate() {
$('.var_txt').each(function() {
if ($.trim($(this).val()) == '') {
$(this).focus();
return false;
}
});
if (!$(".answer:checked").val()) {
alert("boom");
return false;
}
return true;
}
$(document).ready(function() {
$("#add_question").submit(function(e) {
if (validate()) {
alert("good");
}
e.preventDefault();
})
});
HTML 标记
<form id="add_question" method="post" action="">
<table>
<tr>
<td class="var_label">
<input class="answer" type="radio" name="answer" value="a" /> a)
</td>
<td>
<input type="text" class="var_txt" name="var_a" />
</td>
</tr>
<tr>
<td class="var_label">
<input class="answer" type="radio" name="answer" value="b" /> b)
</td>
<td>
<input type="text" class="var_txt" name="var_b" />
</td>
</tr>
<tr>
<td class="var_label">
<input class="answer" type="radio" name="answer" value="c" /> c)
</td>
<td>
<input type="text" class="var_txt" name="var_c" />
</td>
</tr>
<tr>
<td class="var_label">
<input class="answer" type="radio" name="answer" value="d" /> d)
</td>
<td>
<input type="text" class="var_txt" name="var_d" />
</td>
</tr>
<tr>
<td class="var_label">
<input class="answer" type="radio" name="answer" value="e" /> e)
</td>
<td>
<input type="text" class="var_txt" name="var_e" />
</td>
</tr>
</table>
<input type="submit" name="submit" value="ok" />
</form>
Short
What I want to do is following: At first, function validate()
must check all inputs one by one: if they are not empty (or whitespaced) then move to if statement (for checking radio buttons) But if some of inputs empty then stop whole validate function and focus on empty input.
Here is result: http://jsfiddle.net/tt13/y53tv/4/
Just press ok
button, you'll see that it finished first function and fires if too. But I want to exit from whole validate() function in case there is empty field, not only from each() function
Detailed
JS
function validate() {
$('.var_txt').each(function() {
if ($.trim($(this).val()) == '') {
$(this).focus();
return false;
}
});
if (!$(".answer:checked").val()) {
alert("boom");
return false;
}
return true;
}
$(document).ready(function() {
$("#add_question").submit(function(e) {
if (validate()) {
alert("good");
}
e.preventDefault();
})
});
HTML Markup
<form id="add_question" method="post" action="">
<table>
<tr>
<td class="var_label">
<input class="answer" type="radio" name="answer" value="a" /> a)
</td>
<td>
<input type="text" class="var_txt" name="var_a" />
</td>
</tr>
<tr>
<td class="var_label">
<input class="answer" type="radio" name="answer" value="b" /> b)
</td>
<td>
<input type="text" class="var_txt" name="var_b" />
</td>
</tr>
<tr>
<td class="var_label">
<input class="answer" type="radio" name="answer" value="c" /> c)
</td>
<td>
<input type="text" class="var_txt" name="var_c" />
</td>
</tr>
<tr>
<td class="var_label">
<input class="answer" type="radio" name="answer" value="d" /> d)
</td>
<td>
<input type="text" class="var_txt" name="var_d" />
</td>
</tr>
<tr>
<td class="var_label">
<input class="answer" type="radio" name="answer" value="e" /> e)
</td>
<td>
<input type="text" class="var_txt" name="var_e" />
</td>
</tr>
</table>
<input type="submit" name="submit" value="ok" />
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加一个必须为 false 才能继续的标志。
Add a flag that has to be false to continue.
在验证开始时设置一个标志:
varbroken = false;
。在each
内,在return false;
之前添加broken = true;
。然后在你的each
代码之后添加if(broken) return false;
Set a flag at the beginning of your validation:
var broken = false;
. Inside theeach
, just beforereturn false;
, addbroken = true;
. Then after youreach
code addif( broken) return false;
就这样吗?
添加一个变量来检查条件是否为真。
Like that?
Add a variable that checks if the conditions are true.