在一个很长的函数中,我需要在继续编写脚本之前获取用户选项。
这个想法是用这样的东西得到选择的值('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
}
答案 0 :(得分:0)
符合您的语法:
var x = (function myOption(x) { return x; })();
答案 1 :(得分:0)
您需要将选项处理代码放在单独的函数中。
function myMainFct {
// a lot of lines here
if(a_Defined_Value_IsOk)
myCompletion("a");
else {
var choose = "<a onclick=\"myCompletion('a')\">A</a><a onclick=\"myCompletion('b')\">B</a>";
// here I put the <a> choices in a div
}
// end of common main function
}
function myCompletion(option) {
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
}
}
答案 2 :(得分:0)
我终于找到了另一个解决方案。它不是最好的,但它有效。
function myOption(x) { myMainFct(x); } // return to main function when called
function myMainFct(x) {
// 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
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
}