gpt4 book ai didi

jquery - 遍历 jquery 中的嵌套表单元素

转载 作者:行者123 更新时间:2023-11-30 23:52:53 27 4
gpt4 key购买 nike

如果这已经发布了,我很抱歉,我一直在寻找无济于事..

我只是想知道如何在jquery中循环嵌套表单“元素”(元素不仅是像输入标签这样的严格表单元素,还包括其他html元素)。目前我有这段代码可以做到这一点:

$('#'+arguments[i].formid).children().each(function(){ 
var child = $(this);
alert(child.attr('id'));
if(child.is(":input")) { alert(child.attr('id'));
if(child.attr('id')!='') eval("p."+child.attr('id')+"='"+child.attr('value')+"'");
}

if(child.is(":textarea")) {
if(child.attr('id')!='') eval("p."+child.attr('id')+"='"+child.attr('value')+"'");
}
});

当我的表单包含类似这样的其他元素时,它不起作用:

<form>
<div id='tabs'>
<ul>...</ul>
<div id='tab-1'>
<input type='text' id='fname' />
<textarea id='desc' ></textarea>
</div>
</div>
</form>

请帮忙...

最佳答案

您可以使用contents() (并根据需要过滤掉文本节点)或 find('*')获取所有元素,尽管我不喜欢使用通配符。

 $('form').contents()
.filter( function() { return this.nodeType == 1; } )
.each(...);

 $('form').find('*')
.each(...);

关于jquery - 遍历 jquery 中的嵌套表单元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1617694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com