javascript 使用 jQuery 禁用表列的所有控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9451682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:42:53  来源:igfitidea点击:

Disable all controls of a table column using jQuery

javascriptjqueryhtmltablerowrowid

提问by Anupam

I am looking to disable all the elements (controls) of specified table columns (td's) using jQuery.

我希望使用 jQuery 禁用指定表列 (td's) 的所有元素(控件)。

My table looks like the following :

我的表如下所示:

<table>
  <tr>
    <td id="1.1.tcol"></td>
    <td id="1.2.tcol"></td>
    <td id="1.3.tcol"></td>
    <td id="1.4.tcol"></td>
  </tr>
  <tr>
    <td id="2.1.tcol"></td>
    <td id="2.2.tcol"></td>
    <td id="2.3.tcol"></td>
    <td id="2.4.tcol"></td>
  </tr>
</table>

The table is generated dynamically, but this is how it is rendered as. Now each of my <td>has multiple controls like select, checkbox, buttons, but not fixed for every row and column. I am looking to disable all controls of a specified <td>by using it's Id.

该表是动态生成的,但这就是它的呈现方式。现在我每个人<td>都有多个控件,如选择、复选框、按钮,但不是针对每一行和每一列固定的。我希望<td>通过使用它的 Id禁用指定的所有控件。

I used the following jQuery, but it doesn't seemt to do the job :

我使用了以下 jQuery,但它似乎没有完成这项工作:

$('#' + td_id).select('*').each(function(element){element.disabled=true});

$('#' + td_id).select('*').each(function(element){element.disabled=true});

I have also tried the following, still it doesn't seem to work :

我也尝试了以下方法,但似乎仍然不起作用:

$('#' + td_id).attr('disabled', 'false');

$('#' + td_id).attr('disabled', 'false');

Am I doing something wrong? Please help.

难道我做错了什么?请帮忙。

Thanks!

谢谢!

回答by dku.rajkumar

try this

试试这个

$('#' + td_id).find(':input').prop("disabled", true);

:inputselect all the input elements

:input选择所有输入元素

回答by davioooh

if you need to disable all controls in a column (multilpe rows) I think the best way is to assign a class to each tdin that column.

如果您需要禁用一列(多行)中的所有控件,我认为最好的方法是为该td列中的每个控件分配一个类。

For examle if you want to disable controls in the second column, you may do something similar:

例如,如果您想禁用第二列中的控件,您可以执行类似的操作:

<table>
  <tr>
    <td id="1.1.tcol"></td>
    <td id="1.2.tcol" class="col-2"></td>
    <td id="1.3.tcol"></td>
    <td id="1.4.tcol"></td>
  </tr>
  <tr>
    <td id="2.1.tcol"></td>
    <td id="2.2.tcol" class="col-2"></td>
    <td id="2.3.tcol"></td>
    <td id="2.4.tcol"></td>
  </tr>
</table>

Than, using JQuery you can do this:

比,使用 JQuery 你可以做到这一点:

$(".col-2 :input').prop("disabled", true);

It should work...

它应该工作...

回答by Rishabh

I think this should work :

我认为这应该有效:

<table>
<tr><td>
    <input type="text" />
    <input type="radio" />
    <input type="checkbox" />
    <select>
        <option>1233</option>
    </select>
    </td>
</tr>
<tr><td>
    <input type="text" />
    <input type="radio" />
    <input type="checkbox" />
    <select>
        <option>second</option>
    </select>
    </td>
</tr>

Now to disable only first "td" you should use:

现在仅禁用第一个“td”,您应该使用:

$('tr').children().eq(0).find(':input').prop("disabled", true);

If you are using JQuery1.6 or above use prop() else use attr().

如果您使用 JQuery1.6 或更高版本,请使用 prop() 否则使用 attr()。