javascript - 表单 jquery 防止默认

标签 javascript jquery

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

相关文章:

javascript - 淡化更新面板上的背景颜色

javascript - 我想制作一张世界地图,人们点击我的网站就像 Spark 一样。最好的方法是什么?

javascript - 在 extJS 中将数组转换为 JSON

javascript - jquery - 点击动画返回页面顶部

jquery - 不确定状态的复选框在 safari 中不起作用(兼容性问题)

javascript - 使用 Ionic 3 从 JSON 数据筛选器的搜索栏

javascript - 从 HTML div 结构创建多维 JSON 结构

javascript - JavaScript 可以使用多少内存?

javascript - 在 javascript 中使用替换时不应替换属性

javascript - jQuery onclick 无法与验证正常工作