首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Javascript HTML输入数组PHP

Javascript HTML输入数组PHP
EN

Stack Overflow用户
提问于 2018-08-12 17:19:33
回答 1查看 34关注 0票数 0

我在设计一个web应用程序时遇到了输入表单的问题。

我有一个用php动态显示的服务列表,如下所示

代码语言:javascript
复制
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个服务名称,并在每个服务名称旁边打印一个复选框和两个文本框以接受输入。

现在,我要做的是使文本框处于禁用状态,除非我选中该复选框。

这个是可能的吗?我需要些帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-12 18:42:14

基于Gyney Saramali的回答,下面的Javascript将在选中和取消选中复选框时工作。

代码语言: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代码:

代码语言:javascript
复制
$(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.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51807454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档