问题描述
我试图在提交表单之前验证表单。我已经尝试了几个方法,但是我无法获得在preventDefault之后提交的表单。这是我目前的代码:
$('#formquote')。submit(function(e){
e.preventDefault();
console.log('here');
validating = true;
var tval = valText('input [name = contactname]');
var pval = valPhone('input [name = telephone]');
var eval = valEmail('input [name = email]');
console.log(tval );
console.log(pval);
console.log(eval);
if(tval&& pval& eval){
$(#formquote)。unbind('submit');
$(#formquote)。submit();
}
});
控制台日志可以看到预期的输出('here',true,true,true)
表单在第二次提交时按下按钮。我也试过以下建议无济于事:
- > $( #formquote)解除绑定(提交)提交()。; //相同的结果
- > $( #formquote)提交()。 //本身无限循环
- > $( #formquote)[0] .submit(); //非函数错误
- > $('#formquote')。unbind('submit',preventDefault); //'preventDefault'没有定义
任何帮助将不胜感激
验证失败时提交?像这样:
$('#formquote')。submit(function(e){
validating = true ;
var tval = valText('input [name = contactname]');
var pval = valPhone('input [name = telephone]');
var eval = valEmail('input (!(tval&& pval& eval)){
e.preventDefault();
} $;
if b $ b});
I'm trying to validate a form before it is submitted. I've tried a couple of methods but I cant get the form to submit after preventDefault. This is the code I have at the moment:
$('#formquote').submit(function(e){
e.preventDefault();
console.log('here');
validating = true;
var tval = valText('input[name=contactname]');
var pval = valPhone('input[name=telephone]');
var eval = valEmail('input[name=email]');
console.log(tval);
console.log(pval);
console.log(eval);
if(tval && pval && eval){
$("#formquote").unbind('submit');
$("#formquote").submit();
}
});
The console logs you can see output as expected ('here', true, true, true respectively)At the moment the form submits on the second time you press the button. I've also tried the following suggestions to no avail:
-> $("#formquote").unbind('submit').submit(); //same result
-> $("#formquote").submit(); //by itself, infinite loop
-> $("#formquote")[0].submit(); //non function error
-> $('#formquote').unbind('submit', preventDefault); //'preventDefault' not defined
Any help would be appreciated
Instead of always preventing the form from submitting, doing your validation and submitting again if it passes, why don't you just prevent the submission when the validation fails? Something like this:
$('#formquote').submit(function (e) {
validating = true;
var tval = valText('input[name=contactname]');
var pval = valPhone('input[name=telephone]');
var eval = valEmail('input[name=email]');
if (!(tval && pval && eval)) {
e.preventDefault();
}
});
这篇关于在preventDefault之后提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!