我的Javascript中未检测到EventListener

我尝试在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吗?

shushengnan 回答:我的Javascript中未检测到EventListener

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3093746.html

大家都在问