4

单击事件似乎没有在此内存 html 块中的复选框上触发。它确实为按钮而不是复选框触发。

这是小提琴:http: //jsfiddle.net/X2en3/18/

和代码:

使用 .trigger() 触发:使用 .click() 触发:

var working = $('<div><input type="checkbox" id="myBox"/><button id="myButton"/><div>'),    
checkbox = working.find('#myBox'),
button = working.find('#myButton'),
stage;

function boxclicked() {$('#' + stage).append('--checkbox');}
function buttonclicked() {$('#' + stage).append('--button');}

working.delegate('#myBox', {click: boxclicked});
working.delegate('#myButton', {click: buttonclicked});

stage = 'trigger';

checkbox.trigger('click');
button.trigger('click');

stage = 'click';

checkbox.click();
button.click();

这只发生在 Chrome 和 JQuery 1.9.1 上。Firefox/IE 9 和以前版本的 JQuery 都可以正常工作。

任何帮助表示赞赏。

4

1 回答 1

0

From jquery.com

http://api.jquery.com/delegate/

As of jQuery 1.7, .delegate() has been superseded by the .on() method. For earlier versions, however, it remains the most effective means to use event delegation. More information on event binding and delegation is in the .on() method. In general, these are the equivalent templates for the two methods:

Try using the on method

http://api.jquery.com/on/


var $checkbox = $("<input>", { type: "checkbox", id: "myBox" });
var $button = $("<button>", { type: "checkbox", id: "myButton" });

var $container = $("<div>");
$container.append($checkbox);
$container.append($button);

$("body").append($container);

$container.on("click", "#myBox", getCheckboxStatus);
$container.on("click", "#myButton", getButtonStatus);

function getCheckboxStatus() {
    $('#chckBoxVerify').append('Yes');
}

function getButtonStatus() {
    $('#buttonVerify').append('Yes');
}

$checkbox.click();
$button.click();

working example: http://jsfiddle.net/hunter/X2en3/22/

于 2013-04-05T19:58:13.063 回答