Javascript 复选框状态更改不起作用

Javascript 复选框状态更改不起作用,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,在下面的代码中,最初复选框未选中,文本框被禁用。当我单击复选框时,文本框被启用,但在启用后,当我再次选中复选框时,它需要被禁用。但该部分不工作(其他部分) 代码 要使其再次失效,请像下面这样说 $("#txtres").prop("disabled", true); 而不是 $("#txtres").attr("disabled", "disabled"); 演示: 您可以使用“更改”处理程序来实现它 您应该改用.prop().attr()不适用于禁用、只读等布尔属性。@Terry更新了我的

在下面的代码中,最初复选框未选中,文本框被禁用。当我单击复选框时,文本框被启用,但在启用后,当我再次选中复选框时,它需要被禁用。但该部分不工作(其他部分) 代码


要使其再次失效,请像下面这样说

$("#txtres").prop("disabled", true);
而不是

$("#txtres").attr("disabled", "disabled");
演示:

您可以使用“更改”处理程序来实现它


您应该改用
.prop()
.attr()
不适用于禁用、只读等布尔属性。@Terry更新了我的答案。阅读关于它的帮助。在我的代码禁用部分工作,但在其他部分编码不工作。我知道,如果您的最后一个部分缺少
,您没有收到错误吗。
。is(:checked)
=>
。is(':checked')
当我选中它时,我想启用文本框
$("#txtres").attr("disabled", "disabled");
$(function(){
    $("#txtres").attr("disabled", "disabled");
    $("#txtres2").attr("disabled", "disabled"); 
    $("#chknew").click(function () {    
        $("#txtres").attr("disabled", !this.checked);
        $("#txtres2").attr("disabled", !this.checked);          
    });
});
$(function(){
   $("#txtres").attr("disabled", "disabled");
   $("#txtres2").attr("disabled", "disabled");

$('#chknew').change(function () {
   if ($(this).is(":checked")) {
       $("#txtres").removeAttr("disabled");
       $("#txtres2").removeAttr("disabled");
   } else {
       $("#txtres").attr("disabled", "disabled");
       $("#txtres2").attr("disabled", "disabled");
   }
  });
});