我只想在填写所有字段后才触发 submit_form 函数。现在提交表单将被触发。
$('#submit').click(function() {
$('body input').each(function() {
if ($(this).val() == '') {
alert('Please fill in ' + $(this).attr('placeholder'));
$(this).focus();
return false;
}
})
submit_form();
});
function submit_form() {
alert('proceed to sumbit form.');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<input type="" placeholder="name">
<br>
<br>
<input type="text" placeholder="hp">
<br>
<br>
<input type="text" placeholder="email">
<br>
<br>
<button id="submit">Submit</button>
</body>
最佳答案
好的,根据您的代码,您的提交函数总是会调用,所以我为它添加了一个标志,请检查....
$('#submit').click(function() {
var flag = 1;
$('body input').each(function() {
if ($(this).val() == '')
{
alert('Please fill in ' + $(this).attr('placeholder'));
$(this).focus();
flag = 0;
return false;
}
})
if(flag)
{
submit_form();
}
});
关于javascript - 在 javascript 中使用 'continue' 标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267233/