在一个非常长的函数中,我需要在继续执行脚本之前获取用户选项。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”,然后点击 –