简体   繁体   English

如何启用\\禁用表的所有子控件?

[英]How can I Enable\Disable all subcontrols of a table?

I have a checkBox, which when it's checked i want to enable a table and its subcontrols and when it's disabled I want to do the opposite. 我有一个checkBox,当它被检查时,我想启用一个表及其子控件,当它被禁用时,我想做相反的事情。

I have already understand that I have to iterate all subControls of the table, and my question is if there is any nice and generic way of doing it. 我已经明白我必须迭代表的所有子控件,我的问题是,是否有任何好的和通用的方法。

Thanks in advance, 提前致谢,

oz. 盎司

You can do this with jQuery . 你可以用jQuery做到这一点。 Let's say that your HTML looks like this: 假设你的HTML看起来像这样:

<input type="checkbox" onclick="toggleControls(this)"/>Controls Disabled
<table id="myTable">
 <tr>
  <td>Name: <input type="text" /></td>
  <td>Select: <input type="radio" /></td>
 </tr> .....

The toggleControls() function to disable/enable all the controls inside the table ( this means all textboxes, buttons, checkboxes, radio buttons and dropdowns ) looks like this: toggleControls()函数用于禁用/启用表中的所有控件这意味着所有文本框,按钮,复选框,单选按钮和下拉列表 )如下所示:

<script>
   function toggleControls(e){
     $('#myTable input,select,textarea').attr('disabled',e.checked);
   }
</script>

jQuery's css selectors make that one line to disable/enable the controls possible. jQuery的css选择器使一行禁用/启用控件。 With plain old javascript, the function would look like this: 使用普通的旧javascript,函数将如下所示:

var myTable = document.getElementById("myTable");
var controls= myTable.getElementsByTagName("input"); 
// Repeat the previous line for "select" and "textarea"
for(i = 0; i < controls.length; i++) {
    control = controls[i];
    control.disabled = !control.disabled;        
}

It's a lot easier to do with with a selector and a class name using a framework, for example (using Prototype.js). 使用框架(例如使用Prototype.js)使用选择器和类名更容易。

When this item is clicked, enable all items with class of 'mycontrols'. 单击此项目时,启用“mycontrols”类的所有项目。

$('containerDiv').select('.mycontrols').each(function(element){element.disabled=false})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM