基本if/then JS/JQuery-为什么这似乎忽略了if并执行了两次

Basic if/then JS/JQuery - Why does this appear to ignore the IF and execute twice?

本文关键字:if 执行 两次 JQuery- JS then 为什么 基本      更新时间:2023-09-26

我已经尝试了大约20多种不同的变体,所有的道路都会导致IF被完全忽略的结果。想法?

function checkthebox(name,val){
    alert(name + val)
    if(val === 'no');{ 
        alert('The value is ' + val +' for ' + name);
        $('input[name='+name+']').attr('checked', false).button("refresh"); 
    }
    if(val === 'yes');{ alert('The value is ' + val +' for ' + name);
        $('input[name='+name+']').attr('checked', true).button("refresh"); 
    }
}
checkthebox('epcf11','yes');
 if(val === 'no');{ 

应该是

 if(val === 'no') { 

在{之前有分号

分号表示指令的结束,然后括号只创建一个新块。

你的if应该看起来像:

if (val==='yes'){

我也看到了额外的semi,我去测试了它,即使使用了';',条件语句仍然有效http://writecodeonline.com/javascript/