【问题标题】:how to get innerHTML of a Combobox in Javascript?如何在 Javascript 中获取 Combobox 的 innerHTML?
【发布时间】:2020-11-24 17:37:17
【问题描述】:

这是我填充组合框的函数(它的 id 是 bc)

function fillComboBox() {
  for(let i=0; i < SemanticTaskPropertySet.types.length; i++) {
    let option = document.createElement("option");
    option.innerHTML = SemanticTaskPropertySet.types[i].bc;
    option.value = SemanticTaskPropertySet.types[i].bc_id;
    bc.appendChild(option);
    let option2 = document.createElement("option");
    option2.innerHTML = SemanticTaskPropertySet.types[i].ti;
    option2.value = SemanticTaskPropertySet.types[i].ti;
    ti.appendChild(option2);
  }
}

行的值:

option.value = SemanticTaskPropertySet.types[i].bc_id;

我得到 bc.value;

但是我怎样才能得到该行的值呢?

option.innerHTML = SemanticTaskPropertySet.types[i].bc;

我试过了:bc.innerHTML;但这不起作用。 提前致谢!

【问题讨论】:

    标签: javascript combobox innerhtml


    【解决方案1】:

    你可以试试.text属性option元素:

    option.text = SemanticTaskPropertySet.types[i].bc;
    

    【讨论】:

      【解决方案2】:

      option.text 用于option 的内部文本(innerHTML)

      option.valuevalue="1"

      function getTextFunc(x) {
        console.log(x.options[x.selectedIndex].text);
      }
      
      
      let elem = document.getElementById("myElem")
      
      getTextFunc(elem)
      <select id="myElem">
        <option value="1"> Option Text 1</option>
        <option value="2"> Option Text 2</option>
      </select>

      【讨论】:

        【解决方案3】:

        非常感谢! option.text = SemanticTaskPropertySet.types[i].bc; 然后是 bc.options[bc.selectedIndex].text;
        工作得很好

        【讨论】:

          猜你喜欢
          • 2023-01-09
          • 2011-12-02
          • 2012-02-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-26
          • 2011-09-15
          相关资源
          最近更新 更多