我尝试在Javascript中运行以下代码:
function test(){
if(document.querySelector('.js-pricing')){
var checkbox = document.querySelector(".js-pricing");
alert('is working');
checkbox.addEventListener('change',function(){
if(this.checked) {
console.log('is-checked')
} else {
console.log('is not')
}
})
}
}
test();
要知道何时未选中我的复选框,EventListener
不能正常工作,我的控制台中没有console.log,但是我的alert()
正常工作,我猜该元素何时被正确检测到该页面已加载,但无法处理事件侦听器。
也尝试使用document.ready启动事件,但是它不起作用
尝试使用getElementById
时,我得到相同的结果。
这是我输入的html(在翡翠中)行:
input(type="checkbox",name="pricing",id="pricing",checked).switch__input.js-pricing
您知道如何正确运行EventListener
吗?