简体   繁体   English

以编程方式更改复选框的选中

[英]Programmatically change checked of a checkbox

I have the following two checkboxes: 我有以下两个复选框:

<input type="checkbox" id="id3"></input>
<input  type="checkbox" id="id4"></input>

the desired behaviour is that when i click on id3, id4 should adopt. 期望的行为是,当我点击id3时,id4应该采用。

that works fine for the first and second click but aftwerwards not anymore. 这适用于第一次和第二次点击,但不再是后来的。 any idea why? 任何想法为什么?

here my script: 在这里我的脚本:

<script>
       function test2()
       {
         var checked = this.checked;
         if(checked)
            $("#id4").attr("checked", "checked");
         else
            $("#id4").removeAttr("checked");
       }    

       $("#id3").click(test2);
</script>

(or a working dojo here http://dojo.telerik.com/eviTi ) (或者在这里工作的道场http://dojo.telerik.com/eviTi

Please use prop rather than attr and it's advisable to use change event on checkbox instead of the click event. 请使用prop而不是attr,建议在复选框上使用change事件而不是click事件。

attr does DOM manipulation but prop just changes the internal property of any DOM attr执行DOM操作,但prop只是更改任何DOM的内部属性

  <script>


        function test2()
    {
      var checked = this.checked;

      if(checked)
      {
        $("#id4").prop("checked", "checked");
      }
      else
        $("#id4").prop("checked", false);
    }    

    $("#id3").change(test2);

</script>

Use change event(not click ) and play with .prop method instead of .attr 使用change事件(而不是click )并使用.prop方法而不是.attr

Reason: Where both a property and an attribute with the same name exists, usually updating one will update the other, but this is not the case for certain attributes of inputs, such as value and checked : for these attributes, the property always represents the current state while the attribute (except in old versions of IE) corresponds to the default value/checkedness of the input. 原因: 如果属性和具有相同名称的属性都存在,通常更新一个将更新另一个,但输入的某些属性不是这种情况,例如valuechecked :对于这些属性,属性始终表示当前状态,而属性(旧版本的IE除外)对应于输入的默认值/检查。 [ Ref ] [ 参考 ]

 function test2() { $("#id4").prop("checked", this.checked); } $("#id3").change(test2); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <input type="checkbox" id="id3"> <input type="checkbox" id="id4"> 

Use .prop() instead of .attr() 使用.prop()代替.attr()

as like this 像这样

 function test2() { var checked = this.checked; if(checked) $("#id4").prop("checked", "checked"); else $("#id4").removeAttr("checked"); } $(document).ready(function(){ $("#id3").click(test2); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="checkbox" id="id3"></input> <input type="checkbox" id="id4"></input> 

Use .prop() instead of .attr() 使用.prop()代替.attr()

function test2()
    {
      var checked = this.checked;
      if(checked)
      {
        $("#id4").prop("checked", "checked");
      }
      else
        $("#id4").removeAttr("checked");
    }    

I changed your code but the problem is attr() . 我改变了你的代码,但问题是attr() Use prop() instead 请改用prop()

 $("body").on("change","#id3",function(){ if($(this).is(":checked")){ $("#id4").prop("checked","checked"); } else{ $("#id4").removeProp("checked"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="checkbox" id="id3"></input> <input type="checkbox" id="id4"></input> 

You can simply use 你可以简单地使用

function test2()
{
  var checkBox = $("#id4");
  checkBox.prop("checked", !checkBox.prop("checked")); 

}    

$("#id3").click(test2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM