jQuery global variable problem

前端 未结 6 820
伪装坚强ぢ
伪装坚强ぢ 2021-01-22 10:58
var id = $(this).children().html();  // id is 5
$.ajax({
   url: \'ajax.php?id=\' + id,
   success: function(data) {
      id = data;   // id is 1
   }
});
if(id == 1){          


        
6条回答
  •  终归单人心
    2021-01-22 11:25

    Hey a better solution is using the async: false property, something like this:

    var id = $(this).children().html();  // id is 5
    $.ajax({
       url: 'ajax.php?id=' + id,
       async: false,
       success: function(data) {
          id = data;   // id is 1
       }
    });
    if(id == 1){    // id is again 5
       ...
    }
    

    ;)

提交回复
热议问题