這段代碼:
<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 for Android。但在 Firefox 93 for Desktop (Windows 7) 中不會觸發該事件。
我還嘗試了很多不同的語法,例如:
$("#switchService").on('click', function() {
$("#switchService").on('change', function() {
$("#switchService").change(function() {
以上所有內容都適用于我嘗試過的所有瀏覽器,但桌面版 Chrome 93 除外。
它有特定的語法嗎?當然啟用了javascript,因為其他腳本作業正常。
uj5u.com熱心網友回復:
如果您使用如下所示的事件委托,您可以避免@RoryMcCrossan 提到的“競爭條件”型別的困難:
$("body").on("click","#switchService", function() { ... })
.on("change","#switchService", function() { ... })
.on("change","#switchService", function() { ... });
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/453183.html
標籤:javascript html jQuery 谷歌浏览器 复选框
