Javascript 表单jquery阻止默认值

Javascript 表单jquery阻止默认值,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 *

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

/*通用表单处理*/
$('form')。提交(函数(e){
警惕(“你好”);
});
/* 
具体例子
我希望这个处理程序阻止表单正常提交,但同时,我希望它执行上面的代码,并发出警告“hello”
*/
$('form#example').submit(函数(e){
e、 preventDefault();//这将停止通用表单处理(如上)和标准表单提交
});

如果更换

$('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将停止提交):


不,我不想阻止泛型的默认设置。
$('#example').submit(function(e) {
    e.preventDefault(); // this stops the generic form handling (above) and standard form submission
});
$('form#example').submit(function(e) {
    return false;
});