我有下面的代码,需要先验证表单才能提交表单。 但问题是,表单在没有验证的情况下继续提交。
<form action='#' method='post' onsubmit='return validate();'>
function validate()
{
$('form').find(':input:not(:submit,:hidden), select, textarea').each(function(e)
{
$(this).removeClass('redBox');
var rq = $(this).attr('requiredz');
if(rq != undefined)
{
if($(this).val().trim() == '')
{
$(this).addClass('redBox');
$("#errorMsg").html('Red boxes cannont be left empty!');
return false;
}
}
});
});
如何处理循环的返回? 一旦在循环中遇到 return false,请勿提交表单。
最佳答案
试试这个:
function validate()
{
var passes = true;
$('form').find(':input:not(:submit,:hidden), select, textarea').each(function(e)
{
$(this).removeClass('redBox');
var rq = $(this).attr('requiredz');
if(rq != undefined)
{
if($(this).val().trim() == '')
{
$(this).addClass('redBox');
$("#errorMsg").html('Red boxes cannont be left empty!');
passes = false;
}
}
});
return passes;
});
关于javascript - 等待表单提交时循环的返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25074686/