迭代jQuery中的嵌套表单元素 [英] iterate through nested form elements in jquery

查看:88
本文介绍了迭代jQuery中的嵌套表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我只是想知道如何通过嵌套的表单'元素'(元素不仅是输入标签的严格形式元素,而且其他html元素也是如此)在jQuery中。
目前我有这段代码:

  $('#'+ 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>

请帮助...

 函数doStuff(child){$ b (child.is(:input)){
...
}

if(child.is(:textarea)){




函数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());

strong>:我只是想通了,你正在尝试做什么,你可以把它分解成这个

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

您应该阅读关于jQuery的更多信息选择器


im sorry if this was posted already i have been looking to no avail..

I just want to know how to loop through nested form 'elements' (elements being not only the strict form elements like input tags but other html elements as well) in jquery. Currently i have this piece of code to do it:

$('#'+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')+"'"); 
    }
   });

it does not work when my form contains other elements like this:

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

please help...

解决方案

You can do that in a recursive function.

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());

EDIT: I just figured out, what you are trying to do and you can break it down to this

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

You should probably read more about jQuery selectors.

这篇关于迭代jQuery中的嵌套表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆