gpt4 book ai didi

javascript - 表单 jquery 防止默认

转载 作者:行者123 更新时间:2023-11-30 13:27:32 25 4
gpt4 key购买 nike

我想阻止表单提交,但执行其他表单处理程序,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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com