对于 jQuery 1.9.1,参数不会从触发器传递到单击处理程序,而在 jquery 1.7.2 中,参数可以很好地传递。
演示示例:
<!doctype html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<!--script src="http://code.jquery.com/jquery-1.7.2.min.js"></script-->
</head>
<body>
<input id="test" type="checkbox">
<button id='trigger'>trigger</button>
</body>
<script>
$('#test').bind('click',
function (e, data){
if (typeof data == 'undefined') {
alert('no data passed');
return;
}
alert('first passed=' + data.passed1 + ' second passed='+data.passed2);
});
$('#trigger').click(
function (e){
$('#test').trigger('click',{passed1:'first',passed2:'second'});
});
</script>
</html>
当我使用 jquery-1.7.2.js 时,它仍然工作正常。
我错过了什么?请帮助我。
最佳答案
这是一个已知问题:http://bugs.jquery.com/ticket/13353
解决方法是使用 .triggerHandler() 代替,例如:
$('#trigger').click(function (e) {
var $test = $('#test');
$test
.prop('checked', !$test.prop('checked'))
.triggerHandler('click',{passed1:'first',passed2:'second'});
});
JSFiddle:http://jsfiddle.net/antishok/xHVDx/2
无论如何,触发点击都是邪恶的(如果目的只是运行回调)
关于jquery 1.9.1 .trigger() 不将参数传递给点击处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15328894/