开发者

Jquery split function

I have a ajax function as below

<script type="text/javascript">
$(document).ready(function () {

var timer, delay =600000; //5 minutes counted in milliseconds.

timer = setInterval(function(){
    $.ajax({
  type: 'POST',
  url: 'http://localhost/cricruns-new/index.php/score-refresh/fetchdes/index/86/1',
  success: function(html){

    alert(html);
  }
});

},delay);

});
</script>

it outputs the following

total:-370:-wickets:-4:-overs:-50.0:-striker:-Yusuf Pathan:-sruns:-8:-sballs:-10:-sfour:-0:-ssix:-0:-nonstriker:-Virat Kohli:-nsruns:-100:-nsballs:-83:-nsfours:-8:-nssix:-2

i want to开发者_Go百科 split the result using :- and i have to assign even values to the div which has the id as the odd value..


Try this. It uses the split function which is a core part of javascript, nothing to do with jQuery.

var parts = html.split(":-"),
    i, l
;
for (i = 0, l = parts.length; i < l; i += 2) {
    $("#" + parts[i]).text(parts[i + 1]);
}


Javascript String objects have a split function, doesn't really need to be jQuery specific

 var str = "nice.test"
 var strs = str.split(".")

strs would be

 ["nice", "test"]

I'd be tempted to use JSON in your example though. The php could return the JSON which could easily be parsed

 success: function(data) {
   var items = JSON.parse(data)
 }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜