JQuery变量仅在收到警报时才有效

时间:2014-08-23 07:26:46

标签: javascript jquery ajax variables

url = 'test.php';
dynbid = barr[bkey];
dynbcell = bcellarr[bkey];
$.ajax({
    type: "POST",
    cache: false,
    url: url,
    data: {dynbid:dynbid,dynbcell:dynbcell,},
    success: function(html){
        //alert(html);
        //location.reload();
        mybstringt = html;
    }
})
alert(mybstringt);//only works if alerted
mybstring = (window.mybstringt).split(';');
dynbnpc = mybstring[0];
dynbhid = mybstring[1];
dynbhp = mybstring[2];
dynbhpmax = mybstring[3];
dynbvalue = mybstring[4];
dynbname = mybstring[5];
dynbtobuild = mybstring[6];
dynbtobuildat = mybstring[7];
dynbcell = mybstring[8];
dynbimg = mybstring[9];
dynbtype = mybstring[10];

问题是,如果我不警告mybstrint,它将不会被定义,并且代码将不会从下一行运行。我尝试使用localStorage这些变量只是为了它,但它只有在我重新加载页面时才有效。

1 个答案:

答案 0 :(得分:0)

试试这个,将代码移到ajax成功函数

  url = 'test.php';
    dynbid = barr[bkey];
    dynbcell = bcellarr[bkey];
    $.ajax({
        type: "POST",
        cache: false,
        url: url,
        data: {dynbid:dynbid,dynbcell:dynbcell,},
        success: function(html){
        //alert(html);
        //location.reload();
        mybstringt = html;
        //alert(mybstringt);//only works if alerted
        /**Moved your code here **/
        var mybstring = (window.mybstringt).split(';');
        var dynbnpc = mybstring[0];
        var dynbhid = mybstring[1];
        var dynbhp = mybstring[2];
        var dynbhpmax = mybstring[3];
        var dynbvalue = mybstring[4];
        var dynbname = mybstring[5];
        var dynbtobuild = mybstring[6];
        var dynbtobuildat = mybstring[7];
        var dynbcell = mybstring[8];
        var dynbimg = mybstring[9];
        var dynbtype = mybstring[10];
        }
    })