How to iterate through a list of strings returned from a web service using JQuery

后端 未结 4 1789
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-19 17:29

This is my first JQuery experience and I\'m on quite a tight deadline. It\'s a bit of an embarrassing question, but here goes. I\'m calling a web service that returns a l

相关标签:
4条回答
  • 2020-12-19 17:43

    Using Array.split() will produce an array:

    var string = "red,blue,green,orange"
    
    $.each(string.split(','), function(){
      alert(this)
    })
    
    0 讨论(0)
  • 2020-12-19 17:48

    Thanks! I wouldn't have been able to solve it without the feedback. I'm still not completely sure what format resultData is. I did some copy and paste on the code so not sure what data['d'] converts the list into in json terms.

    When I tried the split on resultdata I got this error in Firebug:

    resultData.split is not a function

    In the end I just used the $.each() on resultdata without the split and it worked.

    function onActionCompleted(data) {
    
        var resultData = data['d'];
        $.each(resultData, function() {
            alert(this)
        })
    }
    
    0 讨论(0)
  • 2020-12-19 17:55

    Consider string.split() instead of jQuery:

    var items = results.split(',');
    
    for( var i = 0; i < items.length; i++ ) {
      alert( items[i] );
    }
    
    0 讨论(0)
  • 2020-12-19 18:04

    Sounds like your webservice gave you a csv-string. Split it into an array, wrap jQuery around it and add a callback function for each of the elements like this:

    $(resultData.split(",")).each(function () {
        alert(this);
    });
    
    0 讨论(0)
提交回复
热议问题