开发者

How to stop/pause the execution of a jquery change event from a another onclick event?

I have a jQuery change event which is fired after a textbox was changed,

the event is triggered only when the textbox is loosing the focus.

If the users next thing is pressing the cancel button, then the change event is triggered and then the cancel button's onclick is triggered too, but I want to 'kill' the change event and I want it from the onclick event.

I know that in jQuery you have some kind of control over events.

<input id="change" type="text" />
<input id="cancel" type="button" value="Cancel" onclick="return cancelEvent(this);" />


$(function () 
{
   $('#change').live('change', function() {
    alert('changed');
   });

});

function cancelEvent(e){
    //kill the change event and continue
    alert('cancel');
    return false;
}

There's a solution if I kill then rebind the event :

$('#changeButton').die('change',..).live('change',...); ,

but it has t开发者_开发技巧o be a more efficient solution for that.

Here's a jsbin link where you can play with it!

Hope that's more understandable.

Any contribution to the question are welcome.


Edit #1

Seems like I now understand what you want, so ignore my previous answer. Instead, have a look at this example. Save the value of the control upon focus. Restore the value if cancel button was pressed, even after change was comitted.

Edit #2

Same example with one less event handler

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜