admin管理员组文章数量:1277875
I have to event handlers attached to a form. The first one that fires should stop the other event if a condition is not met.
The code below does not work, as both events will be triggered. Can you help me?
Thanks!
//fires first
$("#myform").submit(function(){
if (some validation) {
alert("You need to make the form valid");
return false;
}
});
//fires second
$("#myform").submit(function(){
//ajax stuff
return false;
});
p.s. I have to this as the ajax stuff is in a plugin that is not changeable. I cannot avoid two event handlers
I have to event handlers attached to a form. The first one that fires should stop the other event if a condition is not met.
The code below does not work, as both events will be triggered. Can you help me?
Thanks!
//fires first
$("#myform").submit(function(){
if (some validation) {
alert("You need to make the form valid");
return false;
}
});
//fires second
$("#myform").submit(function(){
//ajax stuff
return false;
});
p.s. I have to this as the ajax stuff is in a plugin that is not changeable. I cannot avoid two event handlers
Share Improve this question asked Dec 22, 2010 at 9:52 Silviu PostavaruSilviu Postavaru 1,7944 gold badges15 silver badges19 bronze badges2 Answers
Reset to default 8Have a look at event.stopImmediatePropagation()
:
Keeps the rest of the handlers from being executed and prevents the event from bubbling up the DOM tree.
$("#myform").submit(function(event){
if (some validation) {
event.stopImmediatePropagation();
alert("You need to make the form valid");
return false;
}
});
You might also want to use event.preventDefault()
.
Update: Just to clarify: You can rely on the order the event handlers are called. From jQuery's bind
method:
When an event reaches an element, all handlers bound to that event type for the element are fired. If there are multiple handlers registered, they will always execute in the order in which they were bound. After all handlers have executed, the event continues along the normal event propagation path.
The order might not be defined in W3C's original definition but it works with jQuery. Otherwise, the above named function would be unnecessary anyway ;)
In short, no. If you have two event handlers, then you have two event handlers. That said however, there are ways around it.
First, remember that javascript execution is single-threaded. If you define a global variable, say var haveCalledSubmit=0;
, then you can use that to synch your handlers. For example, if you start each handler function with this:
if(haveCalledSubmit == 1)return haveCalledSubmit = 0;
else haveCalledSubmit = 1;
Then only one of the two handlers will be called. You can easily modify it to match some condition, or to deal with more than two functions.
An alternative is to look into event propagation models. This page will give you all the information you need, although Felix has already mentioned an ajax mand that may do what you want.
本文标签: javascriptTwo submit event handlers on a form One must stop the otherStack Overflow
版权声明:本文标题:javascript - Two submit event handlers on a form. One must stop the other - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741225711a2361816.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论