开发者

jQuery Loop from a AJAX Response

开发者 https://www.devze.com 2022-12-23 23:31 出处:网络
I\'m building a tagger. After a user submits a tag, ajax returns: {\"returnmessage\":\"The Ajax operation was successful.\",\"tagsinserted\":\"BLAH, BLOOOW\",\"returncode\":\"0\"}

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

{"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?

Here is the current code:

$("#tag-post").click(functio开发者_JS百科n(){
    // 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');

        }
    });     

});


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");
      });
    }
  });
});


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消