如何停止从onchange事件处理程序执行click事件 [英] how to stop execution of click event from onchange event handler

查看:254
本文介绍了如何停止从onchange事件处理程序执行click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下示例中 http://jsfiddle.net/anu4nits/449d5j8e/

<input type="text" id="in" /> <input type="button" value="click" id="button" /> $('#in').change(function () { console.log('change event'); dialog.dialog("open"); }); $('#button').on('click', function (e) { console.log('click event'); });

<input type="text" id="in" /> <input type="button" value="click" id="button" /> $('#in').change(function () { console.log('change event'); dialog.dialog("open"); }); $('#button').on('click', function (e) { console.log('click event'); });

我附加了两个事件,一个是文本上的onchange事件,另一个是按钮上的click事件.如果我在文本框中写一些东西并单击按钮,则更改事件的处理程序和单击事件的处理程序应分别执行.但是当我在change事件的处理程序中调用modal-dialog时,click事件的处理程序未得到执行,这是可以预期的,因为modal-dialog不允许任何后台活动.但这仅适用于chrome和mozilla.在IE处理程序中,这两个事件都已执行.

I have attached two events one is onchange event on text and other is click event on button. If I write something in textbox and clicks on button then handler of change event and handler of click event should get executed respectively. But as I am invoking the modal-dialog in the handler of change event, handler of click event is not getting executed, which is expected as modal- dialog won't allow any background activity. But this is only with chrome and mozilla. In IE handler for both the events are getting executed.

我希望所有浏览器的行为一致,最好不要在IE中执行点击处理程序.我也不想在全局级别上放置一些标志以进行条件检查.另外,我无法从按钮分离事件,因为我的应用程序按钮ID及其处理程序是在运行时生成的. 还有其他方法可以停止更改事件处理程序中click事件的执行.

I want consistent behaviour for all browsers preferably i dont want click handler execution in IE. I also don't want to put some flag at global level for condition checking. Also I can't detach the event from button as in my application button ID and its handler are generating at runtime. Is there any other way I can stop the execution of click event from the handler of change event.

推荐答案

单击和更改都是独立的事件,它们具有自己的堆栈.因此,您不能停止彼此传播,因为它们的目标元素也不同. 您可以做的是将事件附加在它们的公共父对象上,以便为它们提供一个公共处理程序.在那里,您可以设置标志并执行所需的操作.

Click and Change both are separate events and they have their own stack. So you can't stop propagation of one from another as their target element are also different. What you can do is to attach event at their common parent so that you have a common handler for them. There you can set flag and perform what you want.

这篇关于如何停止从onchange事件处理程序执行click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆