2016-08-05 51 views
0

我试图启用和禁用文本框时复选框检查。 但是这里只有一个文本框在勾选复选框时启用。 如何在勾选复选框时启用和禁用两个文本框? 这里是我的代码:如何启用复选框检查文本框

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

在此先感谢.. !!

回答

1

[0]将只返回你单个DOM元素,因此它工作于单个元素。

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

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

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

它的工作对我来说..thank你..! –

0
$('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); 
+0

谢谢你...... !!! –

+0

该状态将产生错误,因为'[0]'将返回底层DOM元素,并且它们没有'.prop()'方法。另外代码转储也没有用。 – Satpal

+0

谢谢,当然 –

相关问题