javascript Jquery DIV 中的每个输入(有一个表)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11558957/
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 13:40:15  来源:igfitidea点击:

Jquery Each Input Within DIV (Has A Table)

javascriptjqueryhtmlinputforeach

提问by LmC

Im currently trying to get all input elements from a DIV.

我目前正在尝试从 DIV 获取所有输入元素。

$("[required]").each(function() {

});

I have this code which returns every element with a requiredattribute.

我有这段代码,它返回每个具有required属性的元素。

I know within the function of that each()I can use each item like:

我知道在这个功能内each()我可以使用每个项目,例如:

$(this)

So I get the div ID's shown below, and try to get all the inputs from it via:

所以我得到如下所示的 div ID,并尝试通过以下方式从中获取所有输入:

var id = $(this).attr('id');
console.log("### " + id);
console.log($("#" + id + " > :input").length);

Which returns 0 when I have 4 in put elements within that DIV (also theres a table in the div).

当我在该 DIV 中有 4 个放置元素时返回 0(在 div 中还有一个表)。

What I would ideally like to do is for each input element within my div, print its ID.

理想情况下,我想做的是为我的 div 中的每个输入元素打印其 ID。

UPDATED

更新

<div id="contactAddress" required="true">

 <td>Addres line 1:</td>
 <td colspan="2">
 <input type="text"/>
 </td>
 <td>Addres line 2:</td>
 <td colspan="2">
 <input type="text" />
 </td>
 </tr>
</div>

console.log($(this).html());

Shows nothign but if i add anythign outsire the or like below...

不显示任何内容,但如果我在下面添加任何外部或类似内容...

 <div id="contactAddress" required="true">
 <input type="text" />

And run console.log($(this).html());It shows it put not the table?

并运行 console.log($(this).html());它显示它放不表?

Any ideas im using Jquery Mobile

我使用 Jquery Mobile 的任何想法

回答by Undefined

This below will find all inputs inside the div element. It will then leave a log of the id of this element.

下面将找到 div 元素内的所有输入。然后它会留下这个元素的 id 的日志。

$("div > input").each(function() {
  console.log( $(this).attr("id") );
});

if you need the div id containing the inputs you could use .parent()

如果您需要包含可以使用的输入的 div id.parent()

$("input").each(function() {
  console.log( $(this).parent().attr("id") );
});