选择并禁用表单中的每个输入字段,包含在jquery中的表中

前端之家收集整理的这篇文章主要介绍了选择并禁用表单中的每个输入字段,包含在jquery中的表中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在禁用基于复选框的表单…

我在添加disabled属性时遇到问题.

这是我到目前为止所得到的:
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 Dev Console错误
未捕获的TypeError:对象#<一个HTMLInputElement>没有方法’attr’

当我警告.each()中的项目时,它会发出警告[object HTMLInputElement]

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

解决方法

函数不会为您提供jQuery对象.它应该是:
$("#shipInfoTable input").each(function(index,item){
    $(item).attr("disabled",true);
});

(注意我也简化了你的选择器,它仍然有效)
如果你没有对每个项目做任何事情,这也会起作用:

$("#shipInfoTable input").attr("disabled",true);
原文链接:https://www.f2er.com/html/227856.html

猜你在找的HTML相关文章