2

If I have a large form and want to traverse only part of it (elements inside div#traverseMe), what would be the best solution to do this. Here is an example form:

<form id="form1">
 <input type="text" name="text1" />
 <div id="traverseMe">
  <input type="text" name="text2" />
  <input type="text" name="text3" />
 </div>
</form>

I would like to get all (and only) FORM elements that are inside traverseMe. I am currently using a javascript function that loops through all children (document.forms['form1'][i]) of the form with the goal of transforming the form into XML, which is fine for the entirety of the form but overkill considering I only need a portion of it. Is there a suitable jQuery solution for this?

4

2 回答 2

1

尝试 -

$("#traverseMe > :input").each(function() {
  //do someting
})    

演示 - http://jsfiddle.net/ipr101/v3scP/1/

于 2011-10-10T15:22:37.327 回答
1

您可以在选择器中使用它的 ID:

$('#traverseMe input').each(doStuff);

或者您可以将上下文传递给 jQuery 构造函数:

var context = document.getElementById('traverseMe');
$('input', context).each(doStuff);
于 2011-10-10T15:27:37.240 回答