如何检测是否在javascript中选中了复选框

时间:2013-12-19 23:14:54

标签: javascript jquery checkbox

我想检测复选框是否已选中,如果用户取消选中,我现在成功执行此操作,那么我该如何解决? 我的代码就是这个

window.updated = function() {

    var x = document.getElementById("new").checked;
    if(x=true)
    {
        alert("true");
    }
    else
    {
         var tw1 = document.getElementById('tw1');

            tw1.value = '';
            document.getElementById('tw1').readOnly=false;
            //2nd

          tw2 = document.getElementById('tw2');    
            tw2.value = '';
            document.getElementById('tw2').readOnly=false;
            //3rd
         tw3 = document.getElementById('tw3');    
            tw3.value = '';
            document.getElementById('tw3').readOnly=false;
            //4th
         tw4 = document.getElementById('tw4');    
            tw4.value = '';
            document.getElementById('tw4').readOnly=false;
            //5th
         tw5 = document.getElementById('tw5');    
            tw6.value = '';
            document.getElementById('tw5').readOnly=false;
            //6th
         tw6 = document.getElementById('tw6');    
            tw6.value = '';
            document.getElementById('tw6').readOnly=false;
            //7th

      tw7 = document.getElementById('tw7');    
            tw7.value = '';
            document.getElementById('tw7').readOnly=false;
    }
             var ne1 = document.getElementById('ne1'),
          tw1 = document.getElementById('tw1');    
            tw1.value = ne1.value;
            document.getElementById('tw1').readOnly=true
            //2nd
              var ne2 = document.getElementById('ne2'),
          tw2 = document.getElementById('tw2');    
            tw2.value = ne2.value;
            document.getElementById('tw2').readOnly=true
            //3rd
              var ne3 = document.getElementById('ne3'),
          tw3 = document.getElementById('tw3');    
            tw3.value = ne3.value;
            document.getElementById('tw3').readOnly=true
            //4th
              var ne4 = document.getElementById('ne4'),
          tw4 = document.getElementById('tw4');    
            tw4.value = ne4.value;
            document.getElementById('tw4').readOnly=true
            //5th
              var ne5 = document.getElementById('ne5'),
          tw5 = document.getElementById('tw5');    
            tw5.value = ne5.value;
            document.getElementById('tw5').readOnly=true
            //6th
              var ne6 = document.getElementById('ne6'),
          tw6 = document.getElementById('tw6');    
            tw6.value = ne6.value;
            document.getElementById('tw6').readOnly=true
            //7th
              var ne7 = document.getElementById('ne7'),
          tw7 = document.getElementById('tw7');    
            tw7.value = ne7.value;
            document.getElementById('tw7').readOnly=true
    }

如果选中复选框,则会将结算信息输入中的数据放入送货信息输入中。

但是,在取消选中复选框后,我总是会true

1 个答案:

答案 0 :(得分:3)

您不能在此处比较您的分配(x=true)。应该是if (x)

x = true将值true赋给x,然后在if语句中对其进行评估。这就是为什么它总是如此 如果操作数相等,则此x == true返回true 如果操作数相等且类型相同,则x === true返回true