来自AJAX响应的jQuery循环

时间:2022-08-06 20:14:12

I'm building a tagger. After a user submits a tag, ajax returns:

我正在构建一个标记器。用户提交标记后,ajax返回:

{"returnmessage":"The Ajax operation was successful.","tagsinserted":"BLAH, BLOOOW","returncode":"0"}

I want to take the tagsinserted and loop through it, and during each loop take the item in the list and insert it on the HTML page. suggestion on how to do this right?

我想把taginserted和循环遍历它,并在每个循环中获取列表中的项目并将其插入HTML页面。关于如何做到这一点的建议?

Here is the current code:

这是当前的代码:

$("#tag-post").click(function(){
    // Post $('#tag-input').val()
    $.ajax({
        url: '/tags/ajax/post-tag/',
        data: { newtaginput : $('#tag-input').val(), userid : $('#userid').val()},
        success: function(data) {
            // After posting
            alert('done');

        }
    });     

});

2 个解决方案

#1


1  

You can do something like this:

你可以这样做:

$("#tag-post").click(function(){
  $.ajax({
    url: '/tags/ajax/post-tag/',
    data: {newtaginput : $('#tag-input').val(), userid : $('#userid').val()},
    success: function(data) {
      $.each(data.tagsinserted.split(', '), function(i, v) {
        $("<div></div>").text(v).appendTo("#tagHolder");
      });
    }
  });
});

#2


0  

You can loop through the tags by calling data.tags.split(',') and looping through the array it returns.

您可以通过调用data.tags.split(',')循环遍历标记,并循环遍历它返回的数组。

You can insert tags to the page by calling $('<li />').text(tag).appendTo('someSelector').

您可以通过调用$('

  • ').text(tag).appendTo('someSelector')将标签插入页面。

  • #1


    1  

    You can do something like this:

    你可以这样做:

    $("#tag-post").click(function(){
      $.ajax({
        url: '/tags/ajax/post-tag/',
        data: {newtaginput : $('#tag-input').val(), userid : $('#userid').val()},
        success: function(data) {
          $.each(data.tagsinserted.split(', '), function(i, v) {
            $("<div></div>").text(v).appendTo("#tagHolder");
          });
        }
      });
    });
    

    #2


    0  

    You can loop through the tags by calling data.tags.split(',') and looping through the array it returns.

    您可以通过调用data.tags.split(',')循环遍历标记,并循环遍历它返回的数组。

    You can insert tags to the page by calling $('<li />').text(tag).appendTo('someSelector').

    您可以通过调用$('

  • ').text(tag).appendTo('someSelector')将标签插入页面。