第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何將其轉(zhuǎn)換為在用戶更改下拉框時觸發(fā)的回調(diào)?

如何將其轉(zhuǎn)換為在用戶更改下拉框時觸發(fā)的回調(diào)?

吃雞游戲 2023-04-01 15:20:52
我是 Javascript 的新手,正在嘗試制作我的第一個程序。該程序?qū)嵸|(zhì)上為用戶提供了一系列下拉框,他們的選擇將在最后進(jìn)行整理以形成一個連接的文本字符串。我原來的 JSFiddle顯示該程序?qū)σ粋€下拉框工作正常,我現(xiàn)在正試圖將其重新編寫為一個函數(shù),以便可以對多個不同的下拉框重復(fù)此過程。我的 NEW JSFiddle顯示我現(xiàn)在已經(jīng)將我的代碼重寫為一個函數(shù),并接受兩個參數(shù)(categoryOptions - 用于填充選擇框的不同數(shù)組)和(selector - HTML 文檔中的實際選擇目標(biāo)) .// Step 4 - Function which will print the selected value into the content HTML element// Need helping turning this into a callback for the function above.// I want this function to run but only when the select box is changed.function printSelection() {    var finalSelection = selector.value;    console.log(document.getElementById("content").innerHTML = finalSelection);}}populateSelects (progressOptions, progressSelector);我現(xiàn)在想將用戶選擇的值打印到頁面并將其分配給“finalSelection”。不幸的是它說'Uncaught ReferenceError: selector is not defined''但我不確定如何將他們選擇的值傳遞給它。我知道我需要將此作為回調(diào)執(zhí)行,它會在“更改時”發(fā)生,但我不確定如何使用主 populateSelects 函數(shù)執(zhí)行此操作。任何幫助表示贊賞!
查看完整描述

2 回答

?
30秒到達(dá)戰(zhàn)場

TA貢獻(xiàn)1828條經(jīng)驗 獲得超6個贊

您可以創(chuàng)建一個函數(shù),并在select 的onchange上調(diào)用該函數(shù),將其作為參數(shù)傳遞。 this指向調(diào)用該函數(shù)的 html 元素。您可以嘗試以下代碼段。此外,我還對populateSelects進(jìn)行了一些修改,以便能夠在一次調(diào)用中創(chuàng)建所有下拉菜單。


// Step 1 - Store Strings to be passed to categoryOptions Parameter

let progressOptions = ["Just testing!", "XX has made excellent progress", "XX has made good progress", "XX has made poor progress"];

// Behaviour Options

let behaviourOptions = ["XX has excellent behaviour", "XX has good behaviour", "XX has poor behaviour"];

// Attendance Options

let attendanceOptions = ["XX has excellent attendance", "XX has good attendance", "XX has poor attendance"];

// Punctuality Options

let punctualityOptions = ["XX has excellent punctuality", "XX has good punctuality", "XX has poor punctuality"];

// Improvement Options

let improvementsOptions = ["XX should carry on as they have", "XX could make some improvements", "XX must improve"];


// Step 2 - Target the ID; objects to be used in the select below.


let dropDownConfig = [{

    id: "progressDropdown",

    categoryOptions: progressOptions

  },

  {

    id: "behaviourDropdown",

    categoryOptions: behaviourOptions

  },

  {

    id: "attendanceDropdown",

    categoryOptions: attendanceOptions

  },

  {

    id: "punctualityDropdown",

    categoryOptions: punctualityOptions

  },

  {

    id: "improvementsDropdown",

    categoryOptions: improvementsOptions

  },

]


// Step 3 - For Loop Passing the Values from progressOptions as option elements in HTML


function populateSelects(dropDownConfig) {

  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);

    }

  }

}

// Step 4 - Function which will print the selected value into the content HTML element

// Need helping turning this into a callback for the function above.

// I want this function to run but only when the select box is changed.

function printSelection(e) {


  document.getElementById("content").innerHTML+=  e.value+' ';

}


populateSelects(dropDownConfig);

<body>

  <select id="progressDropdown" onchange="printSelection(this)">

    <option>Progress Dropdown</option>

  </select>

  <select id="behaviourDropdown" onchange="printSelection(this)">

    <option>Behaviour Dropdown</option>

  </select>

  <select id="attendanceDropdown" onchange="printSelection(this)">

    <option>Attendance Dropdown</option>

  </select>

  <select id="punctualityDropdown" onchange="printSelection(this)">

    <option>Punctuality Dropdown</option>

  </select>

  <select id="improvementsDropdown" onchange="printSelection(this)">

    <option>Improvements Dropdown</option>

  </select>

  <div id="content"></div>

</body>


查看完整回答
反對 回復(fù) 2023-04-01
?
滄海一幻覺

TA貢獻(xiàn)1824條經(jīng)驗 獲得超5個贊

您可以創(chuàng)建一個函數(shù),您可以在其中傳遞下拉元素并提取其值。

function printSelected(element){
     document.getElementById("content").innerHTML = element.value;
}

并將您的 HTML 更改為:

<select id="progressDropdown" onchange="printSelected(this)">
        <option>Progress Dropdown</option>
    </select>

您可以將此功能與所有其他下拉菜單一起使用。


查看完整回答
反對 回復(fù) 2023-04-01
  • 2 回答
  • 0 關(guān)注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號