jQuery checkbox.prop 无法触发 checkbox.on('change', function)



我尝试使用checkbox.prop('checked',true(来触发checkbox.on('change',function(。但失败了。详细信息如下:https://jsbin.com/jibeboviso/edit?html,js,输出

var checkbox = jQuery("#selectAll");
checkbox.on('change', function() {
alert('changed');
});
// change checkbox prop, cannot trigger `change` event
checkbox.prop('checked', true);

我不知道你想做什么,但当我检查道具功能是否工作时,它可以工作

var checkbox = jQuery('#selectAll');
checkbox.on('change', function() {
alert('changed');
});
// change checkbox prop, cannot trigger `change` event
checkbox.prop('checked', true);
if (checkbox.prop('checked')) {
alert('checked');
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body>
<input type="checkbox" id="selectAll">select all</input>
</body>
</html>

最新更新