如何在复选框选中时启用文本框

How to enable text box on check box check

本文关键字:启用 文本 复选框      更新时间:2023-09-26

选中复选框时,我正在尝试启用和禁用文本框。但这里只有一个文本框在选中复选框时启用。选中复选框时,如何启用和禁用两个文本框?这是我的代码:

<script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
    $('.input_control').change(function () {
        $('input[name=' + this.value + ']')[0].disabled = !this.checked;
    }).change();
});
</script>

提前感谢。。!!

[0]将只返回单个DOM元素,因此它适用于单个元素。

由于您需要修改多个元素的值,因此应该使用.prop( propertyName, value )方法

为匹配的元素集设置一个或多个属性。

$('input[name=' + this.value + ']').prop("disabled", !this.checked);
 $('input[name=' + this.value + ']')[0].prop( "disabled", !this.checked);


编辑:

 $('input[name=' + this.value + ']').first().prop( "disabled", !this.checked);
 $('input[name=' + this.value + ']').prop( "disabled", !this.checked);