在 Chrome 93 中为桌面检测复选框 click/change 事件
Detect checkbox click/change event in Chrome 93 for desktop
此代码:
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="switchService">
<label class="form-check-label" for="switchService">Service</label>
</div>
$("#switchService").on('click', function() {
alert($(this).is(':checked'));
});
在 Firefox 和 Chrome 中适用于 Android。
但在 Firefox 93 for Desktop (Windows 7) 中不会触发该事件。
我还尝试了很多不同的语法,例如:
$("#switchService").on('click', function() {
$("#switchService").on('change', function() {
$("#switchService").change(function() {
以上所有内容都适用于我尝试过的所有浏览器,但 Chrome 93 用于桌面。
是否有特定的语法?
当然 javascript 已启用,因为其他脚本可以正常工作。
如果您使用此处所示的事件委托,则可以避免@RoryMcCrossan 提到的“race-condition”类型的困难:
$("body").on("click","#switchService", function() { ... })
.on("change","#switchService", function() { ... })
.on("change","#switchService", function() { ... });
此代码:
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="switchService">
<label class="form-check-label" for="switchService">Service</label>
</div>
$("#switchService").on('click', function() {
alert($(this).is(':checked'));
});
在 Firefox 和 Chrome 中适用于 Android。 但在 Firefox 93 for Desktop (Windows 7) 中不会触发该事件。
我还尝试了很多不同的语法,例如:
$("#switchService").on('click', function() {
$("#switchService").on('change', function() {
$("#switchService").change(function() {
以上所有内容都适用于我尝试过的所有浏览器,但 Chrome 93 用于桌面。
是否有特定的语法? 当然 javascript 已启用,因为其他脚本可以正常工作。
如果您使用此处所示的事件委托,则可以避免@RoryMcCrossan 提到的“race-condition”类型的困难:
$("body").on("click","#switchService", function() { ... })
.on("change","#switchService", function() { ... })
.on("change","#switchService", function() { ... });