如何在#form中继续循环?

我是Ajax新手,目前我正在尝试为很多div创建Ajax调用。假设我有这样的代码:

    <div id="form">
        <div id="child_1">
            <div id="child_1_select"></div>
        </div>
        <div id="child_2">
            <div id="child_2_select"></div>
        </div>
        <div id="child_3">
            <div id="child_3_select"></div>
        </div>
        <div id="child_4">
            <div id="child_4_select"></div>
        </div>
        <!-- ... -->
    </div>

如何在#form中继续循环?因为我希望所有的child_x_select都有一个Ajax调用。顺便说一下,我不知道会有多少#child_x_select,因为用户可以根据需要添加任意数量的div......非常感谢你考虑我的问题!

You should consider adding a common class to your #child_n_select elements, and use jQuery's .each() method on it :

<div id="form">
    <div id="child_1">
        <div id="child_1_select" class="child_select"></div>
    </div>
    <div id="child_2">
        <div id="child_2_select" class="child_select"></div>
    </div>
    <div id="child_3">
        <div id="child_3_select" class="child_select"></div>
    </div>
    <div id="child_4">
        <div id="child_4_select" class="child_select"></div>
    </div>
    <!-- ... -->
</div>

<script type="text/javascript">
    $('.child_select').each(function() {
        // Write your AJAX call here, using $(this) to select the active element.
    });
</script>

Are you saying that you need to select all of the child_x_select elements after an ajax call? You would simply register the selector on the callback.

var xhr= new XMLHttpRequest();

xhr.onload = function(e) {

  // load all of the children using ajax

  // select the children
  var children = document.querySelectorAll('[id$="_select"]');
  // convert to array and loop
  Array.prototype.slice.call(children).forEach(function(node){
    // do some wacky stuff here
  });
}
xhr.open("GET", url);
xhr.send();

But if you're simply saying you need to loop over each element, this is all you need:

var children = document.querySelectorAll('[id$="_select"]');

Array.prototype.slice.call(children).forEach(function(node){
  // do some wacky stuff here
});

You can loop through the ajax call for creating divs and assign id names in child_x_select format using index(i) value.

$.ajax ({
            type:"GET",
            url: myUrl,
            success:function(data){
                for i in data {
                  $('form').append("<div id='child_"+[i]+"_select'></div>");
                }
        };