简体   繁体   English

jQuery AJAX每个都没有按预期迭代

[英]jQuery AJAX each not iterating as expected

I am using jQuery and I have script as below; 我正在使用jQuery,我有如下脚本;

$(document).ready( function() {
$('.nm').each(function(row) {
$.ajax({
    type: 'POST',
    url: 'test.php',
    data: 'id=' + 1,
    dataType: 'json',
    cache: false,
    success: function(result) {
    $('.title').each(function(index){
      if (result) {

        $(this).html(result[index]);

        } else {
          $(this).html(" ");
          }
      });
  },
});
});
});

The script is suppose to change text in my table; 该脚本用于更改表格中的文本;

<tr class="nm" style="height: 30px">
        <td style="width: 15px;">&nbsp;</td>
        <td class="section" colspan="5">Mix</td>
        <td style="width: 15px;">&nbsp;</td>
    </tr>
    <tr>
        <td id="prev" rowspan="2"><<</td>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
        <td id="next" rowspan="2">>></td>
    </tr>
    <tr>
        <td class="title" >&nbsp;</td>
        <td class="title" >&nbsp;</td>
        <td class="title" >&nbsp;</td>
        <td class="title" >&nbsp;</td>
        <td class="title" >&nbsp;</td>
    </tr>


    <tr class="nm" style="height: 30px">
        <td style="width: 15px;">&nbsp;</td>
        <td class="section" colspan="5">Mix</td>
        <td style="width: 15px;">&nbsp;</td>
    </tr>
    <tr>
        <td id="prev" rowspan="2"><<</td>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
        <td id="next" rowspan="2">>></td>
    </tr>
    <tr>
        <td class="title" >&nbsp;</td>
        <td class="title" >&nbsp;</td>
        <td class="title" >&nbsp;</td>
        <td class="title" >&nbsp;</td>
        <td class="title" >&nbsp;</td>
    </tr>

As you can see, the table has 6 rows, and it is in a 3 - row repeating format. 如您所见,该表有6行,它采用3行重复格式。 The contents are filled by a test.php file which echos a JSON encoded array containing 5 members. 内容由test.php文件填充,该文件回显包含5个成员的JSON编码数组。 In this way, all 5 cells having class="title" get populated successfully. 以这种方式,具有class="title"所有5个单元格成功填充。 But the second set with class="title" is not getting filled. 但是第二组with class="title"并没有被填满。

I made a each(function(row) to repeat the ajax call for every row having class="nm" . But this is not working. I think the [index] is not getting reset after ajax, because the remaining cells are populating if I provide more than 5 members in array. But I want the ajax request to repeat after every row having class="nm" and want to fill the data accordingly. 我创建了each(function(row)为每个具有class="nm"行重复ajax调用。但这不起作用。我认为[index]在ajax之后没有被重置,因为剩下的单元格正在填充,如果我在数组中提供了超过5个成员。但是我希望ajax请求在每行具有class="nm"之后重复并且想要相应地填充数据。

How can I do this? 我怎样才能做到这一点?

$('.title') always selects every .title element. $('.title')总是选择每个 .title元素。 You are not restricting the set to any particular one. 您不是将该集限制为任何特定的集。 You have to make the selector dependent on .nm somehow. 你必须以某种方式使选择器依赖于.nm

For example: 例如:

$('.nm').each(function() {
   var $row = $(this);
    $.ajax({
        type: 'POST',
        url: 'test.php',
        data: 'id=' + 1,
        dataType: 'json',
        cache: false,
        success: function(result) {
            var $titles = $row.next().next().children('.title');
            if (result) { // it's enough to check *once* whether it is set
                $titles.html(function(index){ // easier
                    return result[index];
                });
            }
            else {
               $titles.html("&nbsp;");
            }
        });
    });
});

This should work with the structure you provided. 这应该与您提供的结构一起使用。 $row references each <tr class="nm"> row and $row.next().next() will select second next sibling, which is the row that contains the .title elements. $row引用每个<tr class="nm">行和$row.next().next()将选择第二个下一个兄弟,即包含.title元素的行。
It will break if you change the structure. 如果你改变结构它会破裂。

It would be better if you give the rows containing the .title elements their own class: 如果你给包含.title元素的行提供他们自己的类会更好:

<tr class="something">
    <td class="title" >&nbsp;</td>
    <!-- ... -->
</tr>

Then you can iterate of those instead of the .nm rows, and get the .title elements with $row.children('.title') . 然后你可以迭代那些而不是.nm行,并使用$row.children('.title')获取.title元素。

If your index gets larger than the size of the array returned from your AJAX call, you will refer to an index not present in the array. 如果索引大于从AJAX调用返回的数组的大小,则将引用数组中不存在的索引。 Have you considered using a modulo function on the index? 您是否考虑过对索引使用模数函数? Like: 喜欢:

result[index % result.length]

This should make sure that you never index outside of the array, and that as the index exceeds the size of the array, the [index % result.length] wraps to start from the first element again. 这应该确保你永远不会在数组之外索引,并且当索引超过数组的大小时,[index%result.length]会再次从第一个元素开始包装。

I am not entirely sure if this is what you are looking for... ?!? 我不完全确定这是否是你要找的......?!?

Try assigning THIS into a variable before calling the second function (result) 尝试在调用第二个函数之前将THIS分配给变量(结果)

I think THIS on each function is different from the THIS in the function(result) 我认为每个函数的这个函数与函数中的THIS不同(结果)

each(function(row) {
$.ajax({
type: 'POST',
url: 'test.php',
data: 'id=' + 1,
dataType: 'json',
cache: false,
success: function(result) {
$('.title').each(function(index){
  if (result) {

    $(this).html(result[index]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM