亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么輸入值顯示在控制臺中,但不顯示在下拉框中?

為什么輸入值顯示在控制臺中,但不顯示在下拉框中?

慕尼黑5688855 2023-04-27 17:15:08
我構建了一個接受用戶輸入的基本報告程序,將此輸入添加到一系列添加到下拉列表的段落中。然后將選擇添加到數組中并打印在最終報告中。這是該程序的 JFiddle。正如您將看到的,您的輸入會打印在控制臺中,但不會被拉到下拉列表中。你能幫我弄清楚為什么嗎?function populateSelects(dropDownConfig) {    console.log(`I can get the student name here, but not in the dropdown box. Your name is ${studentName}.`);for (let di = 0; di < dropDownConfig.length; di++) {  for (let i = 0; i < dropDownConfig[di].categoryOptions.length; i++) {    let opt = dropDownConfig[di].categoryOptions[i];    let el = document.createElement("option");    el.text = opt;    el.value = opt;    document.getElementById(dropDownConfig[di].id).add(el);  }}}該函數似乎可以正常工作,但不適用于 studentName/inputStudentName 的值。謝謝!
查看完整描述

1 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

恕我直言,我已經稍微更改了您的代碼以使其更短且更具可讀性。我所做的主要更改是使用占位符文本而不是變量,并在創建選項時將其替換為 studentName。


let options= {

  progress: ['%NAME% has made excellent progress', '%NAME% has made good progress', '%NAME% has made poor progress'],

  behaviour: ['%NAME% has excellent behaviour', '%NAME% has good behaviour', '%NAME% has poor behaviour'],

  attendance: ['%NAME% has excellent attendance', '%NAME% has good attendance', '%NAME% has poor attendance'],

  punctuality: ['%NAME% has excellent punctuality', '%NAME% has good punctuality', '%NAME% has poor punctuality'],

  improvements: ['%NAME% should carry on as they have', '%NAME% could make some improvements', '%NAME% must improve']

}


let dropDownConfig = ["progress", "behaviour", "attendance", "punctuality", "improvements"];


function populateSelects(dropDownConfig) {

  dropDownConfig.forEach(config => {

    let select = document.querySelector(`#${config}Dropdown`);

    options[config].forEach(text => {

        let option = document.createElement("OPTION");

        text = text.replace("%NAME%", studentName);

        option.text = text;

        option.value = text;

        select.appendChild(option);

    })

  })

}


查看完整回答
反對 回復 2023-04-27
  • 1 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號