![](/img/trans.png)
[英]onsubmit still submits form even on return false from ajax verification
[英]AJAX call inside the onsubmit of a form submits the form despite return false
我有只将用于ajax调用的表格。 我通过onsubmit进行了ajax调用,以便能够对必填字段使用自动检查。 即使我在提交时返回假,表单仍会提交。 而且我注释掉了进行ajax调用的语句,表单未提交。 我在下面使用了我提供的。 为什么我不能阻止表单提交。
<form action="" method="post" onsubmit="ajaxReq (); return false;">
<input type="hidden" name="type" value='something'>
<input type="text" name="testText" required>
<input type="submit" value="AJAX_TEST">
</form>
ajaxReq函数如下
//I will be using the data from the form inside data as the data of the ajax call
//Currently data in the form is ignored
function ajaxReq() {
var ajaxSettings = {
'url' : '',
'async' : false,
'type' : 'POST',
'data' : {
'type' : 'SOME_TYPE'
},
'success' : function(data) {
data = JSON.parse(data);
//emptying this part did not change the result
//will be using data here
}
};
$.ajax(ajaxSettings);
return false;
}
给您的表单一个ID,例如id="myform"
然后:
$('#myform').submit(function(e) {
e.preventDefault();
ajaxReq();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.