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

标签 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

最佳答案

您没有在这里比较您的分配(x=true)。应该是 if (x)

x = true 将 true 值分配给x,然后在if 语句中对其进行计算。这就是为什么它总是正确的
如果操作数相等,则 x == true 返回 true。
如果操作数相等并且类型相同,则此 x === true 返回 true

关于javascript - 如何在javascript中检测复选框是否被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693554/

相关文章:

javascript - 表中的 Swiffy 对象对齐

Javascript Promises 就像回调一样嵌套得很深

javascript - 如何在初始化后更改 jquery 插件中的设置

javascript - Jquery 克隆 div 中断事件

ruby-on-rails - 在 rails 中使用 materialize css 框架和简单表单时,复选框不会出现

javascript - 如何设置从数据表中的 Ajax 响应选中的复选框?

javascript - Cycle2 - 转到 : skipping

javascript - jQuery : Change Dropdown value on change event

jquery - 如何将ajax html响应附加到当前div的旁边

c# - 从 DataGrid WPF C# 中的选定行获取特定单元格的数据