我需要控制触发事件的顺序。为了确保我的自定义事件仅在另一个事件完成后触发,我在第一个事件的处理程序中触发了自定义事件。
$(".HwRadioButton").click(function(event) {
//Stuff that needs to happen before custom event
...
//trigger custom event
$(".HwRadioButton").trigger("onAnswerChange");
});
自定义事件绑定:
$(document).ready(function() {
$(".HwRadioButton").bind("onAnswerChange",function(event) {
//custom event stuff
});
});
问题是我有12个元素的类属性值为".HwRadioButton“。事件"onAnswerChange“被触发12次。为什么会这样呢?我甚至需要选择任何元素吗?我只想定义一个自定义事件并显式地触发它。
谢谢!
发布于 2011-03-06 10:08:45
在您的代码中,尝试:
$(".HwRadioButton").click(function(event) {
//Stuff that needs to happen before custom event
...
//trigger custom event
$(this).trigger("onAnswerChange");
});
这将在所单击的一个元素上触发onAnswerChange。
发布于 2011-03-06 10:11:28
你应该像这样使用one
$('.HwRadioButton').one('click', function() {
alert('This will be displayed only once.');
});
发布于 2011-03-06 10:07:15
$(".HwRadioButton").trigger("onAnswerChange")
为该选择器选择的所有项目触发onAnswerChange
。仅为$(this).trigger("onAnswerChange")
触发该事件。
https://stackoverflow.com/questions/5208157
复制相似问题