jQuery:防止默认的浏览器表单提交行为,但使用jQuery提交表单?
问题描述:
$('#search_form').submit(function(e) {
//e.preventDefault();
return false;
})
这很好,可以防止在按Enter键时提交表单.
This works just fine to prevent form submission when pressing Enter.
但是即使在某些情况下,我也想使用jquery提交表单.
However even I have this I want to submit the form with jquery if certain circumstances are true.
$('#search_form').submit(function(e) {
return !!e.submit;
});
function ...
if (e.keyCode == 13) {
if (blablabla) {
... // do something
} else {
$('#search_form').submit({submit:true}); //doesn't work
console.log('submitted'); //this does successfully get fired
}
}
如果我按Enter键,则不会提交表单,但是会在控制台中登录!
If I press enter the form doesn't get submitted, but the log in in the console happens!
答
$('#search_form').submit(function(e) {
if($('.errors').is(':visible')){
e.preventDefault();
// do something else here// some errors are visible :)
return false;
}else{
}
})