jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了迭代jquery中的嵌套表单元素大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我很抱歉,如果这已经发布我一直在寻找无济于事..

我只想知道如何在jquery中循环嵌套表单’elements'(元素不仅是输入标签之类的严格表单元素,还包括其他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>

请帮忙…

解决方法

您可以在递归函数中执行此操作.

function dostuff(child) {
  if(child.is(":input")) {
     ...
  }

  if(child.is(":textarea")) {
    ...
  }
}

function walk(children) {
  if (typeof children == "undefined" || children.size() === 0) {
    return;
  }
  children.each(function(){
    var child = $(this);
    if (child.children().size() > 0) {
      walk(child.children());
    }
    dostuff(child);
  }
}

walk($('#'+arguments[i].formid).children());

编辑:我只是想通了,你想要做什么,你可以把它分解为此

var p = {};
$('#'+arguments[i].formid + " input[id],#"+arguments[i].formid + " textarea[id]").each(function(){
  var child = $(this);
  p[child.attr("id")] = child.attr("value");
});

你应该阅读更多关于jQuery selectors内容.

大佬总结

以上是大佬教程为你收集整理的迭代jquery中的嵌套表单元素全部内容,希望文章能够帮你解决迭代jquery中的嵌套表单元素所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。