在一個非常長的函數中,我需要在繼續執行腳本之前獲取用戶選項。JavaScript調用子函數並從函數內部返回
的想法是像這樣的東西來獲得已選定的值( 'A' 或 'B'):
function myMainFct {
// a lot of lines here
if(a_Defined_Value_Is_Ok) { var option="a"; }
else {
var choose = "<a onclick=\"myOption('a')\">A</a><a onclick=\"myOption('b')\">B</a>";
// here I put the <a> choices in a div
// now the subfunction
function myOption(x) { return x; }
// what will happen when I'll get the value
if (x) { var option=x; }
}
if (option=="a") {
// let's continue the script for users with "a" option
}
else if (option=="b") {
// let's continue the script for those with "b" option
}
// end of common main function
}
對不起,它不起作用。 if(x)alert(x)沒有任何反應:-( –
var x =(function myOption(x){alert(x); return x;})();發送警報「Undefined」,然後點擊 –