weixin_33688840 2013-12-13 18:46 采纳率: 0%
浏览 338

在jQuery中保存变量

hey there i have this jquery combined with ajax:

var username = $("#username").val();
if(username){
    $.ajax({
        url: 'check.php',
        data: {data: JSON.stringify(username)},
        type: 'POST',
        dataType: "json",
        success: function (data) {
            if(data.result == 1) {
                var name = "content";   // SAFE anything in global variable?
            }
        }
    });
} else {
    alertify.error( "You forgot to type your username" );
}    


if(name) {       
    // if global variable is set => do something
} else {         
    // if not => do something else
}

i want to check if the data.result = 1, and thats the only way i could imagine to find out if data.result returned is 1.

EDIT:

I dont want to check it via alert, because i will need to work with this global variable later, anybody could help me with my problem? greetings

EDIT :

$(document).ready(function(){
  $("#submitto").submit(function(e){

    e.preventDefault();

    var username = $("#username").val();
    var name;
    if(username){
        $.ajax({
            url: 'check.php',
            data: {data: JSON.stringify(username)},
            type: 'POST',
            dataType: "json",
            success: function (data) {
                if(data.result == 1) {
                    name = "content";
                }
            }
        });
    } else {
        alertify.error( "You forgot to type your username" );
    }    

    if(name) {
        alert("ok")
    } else {
        alert("not ok")
    }
  });
});
  • 写回答

1条回答 默认 最新

  • weixin_33701251 2013-12-13 18:51
    关注

    Declare a variable before the ajax() call and assign to it inside your success handler:

    var result;
    var username = $("#username").val();
    if(username){
        $.ajax({
            url: 'check.php',
            data: {data: JSON.stringify(username)},
            type: 'POST',
            dataType: "json",
            success: function (data) {
                result = data.result;
            }
        });
    } else {
        alertify.error( "You forgot to type your username" );
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?