gpt4 book ai didi

javascript - 复选框状态更改不起作用

转载 作者:行者123 更新时间:2023-11-28 01:02:20 24 4
gpt4 key购买 nike

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

 $(function(){
$("#txtres").attr("disabled", "disabled");
$("#txtres2").attr("disabled", "disabled");

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

最佳答案

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

$(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");
}
});
});

关于javascript - 复选框状态更改不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25473659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com