简体   繁体   English

使用jQuery和AJAX填充表

[英]Populate table using jQuery and AJAX

I have a table as below; 我有一张桌子,如下:

<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 class="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 class="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">cat</td>
    <td style="width: 15px;">&nbsp;</td>
</tr>
<tr>
    <td class="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 class="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>

I am using AJAX with jQuery to populate table contents. 我在jQuery中使用AJAX来填充表内容。 I have a script as below; 我有一个如下的脚本;

$('.next').click(function() {
  var $nxt = $(this);
  var $titlex = $nxt.prev().parent('.title');
  $.ajax({
          type: 'POST',
          url: 'ajax.php',
          data: 'id=2&dir=mix',
          cache: false,
          success: function(result3) {
            $titlex.eq(index).html("XX");
          },
        });

});

which is supposed to change contents in class="title" in its corresponding row. 这应该更改其相应行中class="title"中的内容。 The php file ajax.php will return an array of data, containing 5 members in JSON. php文件ajax.php将返回一个数据数组,其中包含JSON中的5个成员。 I want to populate the corresponding row's title cells using this method. 我想使用此方法填充相应行的标题单元格。 the other row's contents should not be changed. 另一行的内容不应更改。

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

For starters, I think you want to change: 对于初学者,我认为您想更改:

var $titlex = $nxt.prev().parent('.title');

to

var $titlex = $nxt.parent().next().children('.title');

see the fiddle 看到小提琴

I simplified things and had to take out the ajax but now that it is selecting the right element can you take it from there? 我简化了事情,不得不取出ajax,但现在它正在选择正确的元素,您可以从那里获取它吗?

$nxt.prev()

is to my mind : 在我心中:

<td class="content">&nbsp;</td>

Then : 然后 :

.parent('.title')

Gives you nothing because the parent has no class named "title" (just a no attribute tr tag). 因为父级没有名为“ title”的类(没有属性tr标记),所以您什么也没得到。

First of all, say that you are receiving JSON : 首先,假设您正在接收JSON:

$.ajax({
          type: 'POST',
          url: 'ajax.php',
          data: 'id=2&dir=mix',
          cache: false,
          dataType: "json",
          success: function(result3) {
            //your stuff here
          },
        });

Than wouldn't that be easier for you to give an ID with the index of the tag you want to fill, and then do a for loop on your json result. 给您一个ID与要填充的标签的索引,然后对json结果执行for循环,这比这更容易。 Before that, you could access to the right tr parent node by and access all your title elements: 在此之前,您可以通过访问正确的tr父节点,并访问所有标题元素:

var trTitle = $(this).parent().next();

Then here are you titles : 然后是您的标题:

trTitle.find('.title');

I have tested nothing, but I think this works. 我没有进行任何测试,但是我认为这可行。

Here you go: 干得好:

http://jsfiddle.net/fehays/QNXXf/ http://jsfiddle.net/fehays/QNXXf/

$(function() {
    $('.next').click(function() {
        var $nxt = $(this);
        var $titlex = $nxt.parent().next().find('.title');
        var result3 = {"data":["value1","value2","value3","value4","value5"]};

        $.each(result3.data, function(index, value) {
            $titlex.eq(index).html(value);
        });
    });
});

I removed the ajax call and just used a json object containing an array. 我删除了ajax调用,只使用了一个包含数组的json对象。 Also note that your selector for finding the .title elements was wrong. 另请注意,用于查找.title元素的选择器是错误的。 Needs to be something like this: 需要是这样的:

var $titlex = $nxt.parent().next().find('.title');

Here is a fiddle with what you want : 这是您想要的东西的小提琴:

http://jsfiddle.net/g9ZZr/1/ http://jsfiddle.net/g9ZZr/1/

$('.next').click(function() {
   var $nxt = $(this);
   var $titlex = $nxt.parent().next().children();

  //The data you receive from your webservice.
  var myArray = ['title1','title2','title3','title4','title5'];
  $titlex.each(function(index,elem)
  {
     $(elem).html(myArray[index]);
  });

});

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

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