2010-02-27 34 views
8

我禁用基于一个复选框形式禁止在表单中输入的每个领域,裹在一张桌子选择和jQuery的

我无法加入禁用属性。

这里是我走到这一步: HTML:

<table id="shipInfoTable"> 
    <tr> 
    <td>Name:</td> 
    <td><input type="text" name="name" /></td> 
    </tr> 
    ... 
</table> 

的Javascript选择/属性操作(jQuery的):

$("#shipInfoTable tbody tr td input").each(function(index, item){ 
    item.attr("disabled", true); 
}); 

Chrome浏览器开发控制台错误: Uncaught TypeError: Object #<an HTMLInputElement> has no method 'attr'

当我警告item.each()之内它会提醒[object HTMLInputElement]

不太确定如何正确选择输入元素。我究竟做错了什么?

回答

12

该函数不会给你一个jQuery对象。它应该是:

$("#shipInfoTable input").each(function(index, item){ 
    $(item).attr("disabled", true); 
}); 

(请注意,我还简化了你的选择,它仍然有效)
如果你没有做任何事情ELES与每个项目,这也能发挥作用:

$("#shipInfoTable input").attr("disabled", true); 
+1

请注意:在当前版本的jQuery中,这被改为'.prop(“disabled”,true)'。我很确定'attr'没有做的事是有意的。 – Kobi 2013-02-20 10:52:10

4

....

$("#shipInfoTable tbody tr td input").each(function(){ 
    $(this).attr("disabled", true); 
}); 
0

的问题是在。每个()函数缺乏$()...

$("#shipInfoTable tbody tr td input").each(function(index, item){ 
    $(item).attr("disabled", true); 
});