我在设计一个web应用程序时遇到了输入表单的问题。
我有一个用php动态显示的服务列表,如下所示
while($row=$result->fetch_assoc())
{
extract($row);
echo "<td>$service</td>";
echo "<td><input type='checkbox' name='ac[]'>'</td>";
echo "<td><input type='text' name='as[]' disabled>'</td>";
echo "<td><input type='text' name='ad[]' disabled>'</td>";
}
现在要做的是依次打印至少10个或20个服务名称,并在每个服务名称旁边打印一个复选框和两个文本框以接受输入。
现在,我要做的是使文本框处于禁用状态,除非我选中该复选框。
这个是可能的吗?我需要些帮助。
发布于 2018-08-12 18:42:14
基于Gyney Saramali的回答,下面的Javascript将在选中和取消选中复选框时工作。
$('.checkbox').click(function(){
var state = $(this).parents('td').siblings('td').children('input[type=text]').attr('disabled');
$(this).parents('td').siblings('td').children('input[type=text]').attr('disabled', !state);
});
要解释jQuery代码:
$(this) refers to the check box that was clicked.
.parents('td') refers to the parent object of the checkbox (IOW, the td containing the checkbox).
.siblings('td') refers to the siblings of the td.
.children('input[type=text]') refers to the contents of the td's, just the input fields of type text.
.attr('disabled',!state) sets the state of the disabled attribute. The variable state is the current state of the text fields and the ! invert the value. If false, it makes it true, and if true, makes it false.
https://stackoverflow.com/questions/51807454
复制相似问题