我有一個語言下拉菜單,我嘗試提醒用戶必須先選擇一種語言,然後再進一步處理,如languageOption可以變成字符串並進入數組。但它不工作,我不明白爲什麼。我試過了:下拉選擇值並不成爲真正的價值,並在JavaScript中工作
alert("You didn't choose any language.");
console.alert("You didn't choose any language.");
console.log("You didn't choose any language.");
但他們都不起作用。
而且我認爲另一種方式來解決這個問題,這是我做
<option value="1" selected>English (American)</option>
但隨後的值=「1」不會成爲變量「languageOption」的一個值。所以當我在控制檯中看到時,陣列不響應,也不知道該怎麼做。我不明白爲什麼數組沒有響應。
任何幫助,我將不勝感激。 下面是我的代碼:
<select id="languageSelection" style=display:none>
<option value="">Choose a language</option>
<option value="1">English (American)</option>
<option value="2">Chinese (Mandarin)</option>
<option value="3">Japanese</option>
</select>
var audioSrc = "sound/"
var audioType = ".wav";
// default number of random question, if user this used the dropdown
var default_numFollowUp = 4;
// default delai (seconds) between random questions, if user this used the dropdown
var default_secFollowUp = 10;
// Create the audio element
var audioElement = document.createElement('audio');
var endingArr = [];
var runThroughArr = [];
var randomArr = [];
var languageOption = parseInt($("#languageSelection").val());
$("#languageSelection").on("change", function(){
languageOption = $(this).val(); // Make languageOption value be string
//if(languageOption.length==0){languageOption=1;}
console.log("langugeOption is " + languageOption);
console.log("Language changed to: "+ $(this).find("option").eq($(this)[0].selectedIndex).text() + " (Index: "+languageOption+")");
console.log(typeof(languageOption)); // Outputs string
endingArr = [];
runThroughArr = [];
randomArr = [];
if(languageOption === ""){
alert("You didn't choose any language.");
console.alert("You didn't choose any language.");
console.log("You didn't choose any language.");
}
else if(languageOption === "1"){
console.log("English");
for(i = 0; i < intro_playList.length; i++){
if(intro_playList[i].stage === "ending"){ endingArr.push(i); }
if(intro_playList[i].runThrough){ runThroughArr.push(i); }
if(intro_playList[i].random){ randomArr.push(i); }
}
}
else if (languageOption === "2"){
console.log("Chinese");
for(i = 0; i < intro_playList_chi.length; i++){
if(intro_playList_chi[i].stage === "ending"){ endingArr.push(i); }
if(intro_playList_chi[i].runThrough){ runThroughArr.push(i); }
if(intro_playList_chi[i].random){ randomArr.push(i); }
}
}
});
「intro_playlist」的值是什麼?控制檯消息和警報正在顯示,當我嘗試你的代碼時,它只是產生錯誤,「intro_playlist是未定義的; –
intro_playlist是一堆音頻文件,相同intro_playList_chi具有不同特性,例如 「關鍵」: 「2_how_old」, 「delayAfter」:6, 「階段」: 「前奏」, 「runThrough」:真, 「隨機「:錯誤。當我嘗試查看languageOption時,它是一個字符串類型。 – Johnny88520
當我嘗試選擇英文或中文時,在單擊Enter按鈕後工作。如果我沒有點擊任何內容,那麼它不會默認英文。我想知道它是否需要用戶互動 – Johnny88520