![](/img/trans.png)
[英]How to call a specific function from inside a subfunction in javascript?
[英]Javascript calling a subfunction and get return from inside a function
在很長的功能中,我需要先獲得用戶選項,然后才能繼續執行腳本。
這個想法是用這樣的東西來獲得選擇的值('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
}
與您的語法保持一致:
var x = (function myOption(x) { return x; })();
您需要將選項處理代碼放在單獨的函數中。
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
}
}
我終於找到了另一個解決方案。 它不是最好的,但是可以。
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.