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

发布于 2024-08-23 08:31:14 字数 665 浏览 2 评论 0原文

我正在禁用基于复选框的表单...

我在添加禁用属性时遇到问题。

这是我到目前为止得到的: 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 开发控制台错误: 未捕获类型错误:对象#没有方法 'attr'

当我警告 .each() 中的 item 时,它会警告 [object HTMLInputElement]

不是非常确定如何正确选择输入元素。我做错了什么?

I am disabling a form based on a checkbox...

I am having trouble adding the disabled attribute.

here is what I got so far:
HTML:

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

Javascript selector/attribute manipulation(jquery):

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

Chrome Dev Console error:
Uncaught TypeError: Object #<an HTMLInputElement> has no method 'attr'

When I alert out the item within the .each() it alerts [object HTMLInputElement]

Not quite sure how to select the input element properly. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

输什么也不输骨气 2024-08-30 08:31:14

该函数不会为您提供 jQuery 对象。它应该是:(

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

请注意,我还简化了您的选择器,它仍然有效)
如果您没有对每个项目执行任何操作,这也将起作用:

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

The function will not give you a jQuery object. It should be:

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

(note that I also simplified your selector, it still works)
If you aren't doing anything eles with each item, this will work as well:

$("#shipInfoTable input").attr("disabled", true);
魄砕の薆 2024-08-30 08:31:14

....

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

....

$("#shipInfoTable tbody tr td input").each(function(){
    $(this).attr("disabled", true);
});
淡莣 2024-08-30 08:31:14

问题是 .each() 函数中缺少 $() ...

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

the issue is the lack of $() in the .each() function...

$("#shipInfoTable tbody tr td input").each(function(index, item){
    $(item).attr("disabled", true);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文