Event.preventDefault() failing in Jquery on all browsers
Simple question. I have the following HTML:
<a rel="nofollow" id="claim-job-link" data-method="post" class="button green claim-job" href=""><span class="button"> <span class="text"> Claim Job </span> </span></a>
And the following Jquery:
$("#claim-job-link").bind('click', function(event) {
event.preventDefault();
});
which is called with no problems. Adding a breakpoint to the preventDefault shows that it is called. However, the browser is still redirected in both Chrome and Firefox. Anyone faced anything similar?
Edited to add
It looks like the the event is prevented at first and then jquery enters this code block:
for ( i = 0, l = elems.length; i < l; i++ ) {
match = elems[i];
if ( maxLevel && match.level > maxLevel ) {
break;
}
event.currentTarget = match.elem;
event.data = match.handleObj.data;
event.handleObj = match.handleObj;
ret = match.handleObj.origHandler.apply( match.开发者_运维问答elem, arguments );
if ( ret === false || event.isPropagationStopped() ) {
maxLevel = match.level;
if ( ret === false ) {
stop = false;
}
if ( event.isImmediatePropagationStopped() ) {
break;
}
}
}
return stop;
}
Then when it exits, it runs the does the redirect call.
Thanks
Works fine for me. http://jsbin.com/ateje4/edit There must be something else happening. I would create a reduction from your page to track it down.
Note: bind
is additive. Try this:
$("#claim-job-link").unbind('click').bind('click', function(event) {
event.preventDefault();
});
Thanks for your help guys. I figured it out though. In a rails app, when you call either :remote=>true or :method=>:xxx, it adds a tag to the link. This tag is processed by the rails app in such a way that the event.preventDefault call doesn't work. By removing the :method attribute, the call now works. Go figure. Thanks again for all your help.
精彩评论