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

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

如何在選擇新選項后立即更改 html 元素的文本內容?

如何在選擇新選項后立即更改 html 元素的文本內容?

月關寶盒 2023-09-28 16:43:06
當在 Javascript 中選擇新選項時,我試圖更改 span 元素。這是html代碼:<span id="month"></span>(...)<option id="plan_option".....這是我的 javascript 代碼,當前僅在頁面加載時顯示文本:window.onload = function month_freq() {          var id = document.getElementById("plan_option").value;          var freq = '';          if (id == 5144746){            freq = 'ogni mese';          } else{            freq = 'ogni due mesi';          }          document.getElementById("month").innerHTML = freq;        }那么,我應該創建一個在選項更改或 idk 時調用的新函數嗎?任何幫助表示贊賞,謝謝!
查看完整描述

2 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

人們只想聽一個選擇元素的變化。


因此,人們需要以某種方式識別這個特定的選擇元素,而不是它的每個選項元素。后者不需要具有name- 或id- 屬性,而是需要 -value屬性。


然后,我們會實現一個事件處理程序,該處理程序會讀取當前所選選項的值,并將該值寫入所需/相關的 html 元素。


還需要為前面提到的 select 元素提供事件偵聽/處理。


此外,我們希望在加載/渲染時將默認選定值與顯示元素同步。


筆記


出于安全原因,人們并不真正希望通過innerHTML...呈現文本值,在這種情況下,textContent寫訪問就可以很好地完成這項工作。


function handleMonthOptionChangeForRelatedDisplay(evt) {


  const elementDisplay = document.querySelector('#month');

  const elementSelect = evt.currentTarget;


  if (elementDisplay && elementSelect) {


    const elementSelect = evt.currentTarget;

    const selectedIndex = elementSelect.selectedIndex;


    elementDisplay.textContent = elementSelect[selectedIndex].value

  }

}


function initMonthOptionChange() {

  const elementSelect = document.querySelector('#month-options');

  elementSelect.addEventListener('change', handleMonthOptionChangeForRelatedDisplay);

}


// window.onload = function () {

//   handleMonthOptionChangeForRelatedDisplay({

//     currentTarget: document.querySelector('#month-options')

//   });

//   initMonthOptionChange();

// }


handleMonthOptionChangeForRelatedDisplay({

  currentTarget: document.querySelector('#month-options')

});

initMonthOptionChange();

<select name="plan_option" id="month-options">

  <option value=""></option>

  <option value="Ogni Mese">ogni mese</option>

  <option value="Ogni due Mesi" selected>ogni due mesi</option>

</select>


<p id="month"></p>

如果OP必須呈現與選項元素的屬性不同的特定于選項的文本值,value仍然可以通過特定于選項的data屬性提供此信息的方法,以便使處理程序實現保持通用(沒有任何額外的和特定情況的比較邏輯)盡可能......


function displayBoundBillingFrequency(evt) {


  const elementSelect = evt.currentTarget;

  if (elementSelect) {


    const selectedOption = elementSelect[elementSelect.selectedIndex];


    // `this` equals the bound billing-frequency display-element.

    this.textContent = (selectedOption.dataset.billingFrequency || '');

  }

}


function mainInit() {


  const planOptions = document.querySelector('#plan-options');

  const frequencyDisplay = document.querySelector('#plan-billing-frequency');


  if (planOptions && frequencyDisplay) {


    const displayBillingFrequency = displayBoundBillingFrequency.bind(frequencyDisplay);


    // synchronize display data initially.

    displayBillingFrequency({

      currentTarget: planOptions,

    });


    // initialize event listening/handling

    planOptions.addEventListener('change', displayBillingFrequency);

  }

}


mainInit();

<select name="plan_option" id="plan-options">

  <option value=""></option>

  <option value="541758" data-billing-frequency="ogni mese" selected>First Option</option>

  <option value="752649" data-billing-frequency="ogni due mesi">Second Option</option>

  <option value="invalid">Invalid Option</option>

</select>


<p id="plan-billing-frequency"></p>


查看完整回答
反對 回復 2023-09-28
?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

您應該檢查HTMLElement:change 事件

var planSelectElem = document.getElementById("plan_select"); // <select id="plan_option_select"></select>


planSelectElem.onchange = month_freq;


// OR


planSelectElem.addEventListener("change", month_freq);


查看完整回答
反對 回復 2023-09-28
  • 2 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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