Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

javascript - How can I pass this specific variable I'm using jquery-ajax

I want to pass the "passedThisValue" to my "start_battle" function and use the "start_battle" function in my "Rematch". But the modal just hangs why is this happening? what could be wrong? Please help! :) Thank you.

CODE:

function start_battle(){
    $.ajax({
        data: {
            receivePassedValue: passedThisValue
        },
        success: function(data){

        }
    });
}
$("#start_battle").click(function() {
    $.ajax({
        success: function(data){
            var toAppend = '';
            if(typeof data === "object"){
                var passedThisValue = '';
                for(var i=0;i<data.length;i++){
                    passedThisValue = data[i]['thisValue']; 
                }

                start_battle(); // can I still get the passedThisValue?

            }
        }
    });
    $("#battle").dialog({
        modal:true,
        buttons: {
            "Rematch": function(){
                start_battle(); // can I still get the passedThisValue?
            }
        }
    });
    $("#battle").show(500);
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you call a function, you don't use function start_battle();, you just use start_battle();.

When you pass a value to a function, you need to use this syntax: start_battle(param1, param2);.

When you want to get a value from a function, you need to return it in the function, like so:

function start_battle(param1) {
    // Do something
    return param1;
}

When you want to store a returned value from a function, you do something like: var returned = start_battle(param1);

And the fact that you don't know why the modal just hangs, means that you didn't check the browser's error console, which can hold some pretty important information on what's wrong. Try checking that and posting here so we can see the current problem


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...