我想阻止表单提交,但执行其他表单处理程序,preventDefault 停止所有。
举个例子:
<form id="example">
<input type="text" name="name" />
</form>
<form id="example2">
<input type="text" name="name2" />
</form>
/* generic form handling */
$('form').submit(function(e) {
alert('hello');
});
/*
specific example
i want this handler to prevent the form from submitting normally, but at the same time, i want it to execute the above code with the alert "hello"
*/
$('form#example').submit(function(e) {
e.preventDefault(); // this stops the generic form handling (above) and standard form submission
});
最佳答案
尝试返回 false(http://api.jquery.com/submit/声明返回 false 将停止提交):
$('form#example').submit(function(e) {
return false;
});
关于javascript - 表单 jquery 防止默认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7973393/