【问题标题】:Finding which <Option> is selected in <select> (without JQuery)查找在 <select> 中选择了哪个 <Option>(没有 JQuery)
【发布时间】:2012-12-29 07:39:33
【问题描述】:

我有以下元素:

<select id="color" name="colorId" class="btn secondary">
    <option value="347366" selected="selected">Purple</option>
    <option value="56634">White</option>
</select>

我想知道选择了哪个选项:

以下只给我默认值:

document.querySelector('#color option[selected="selected"]')

(我知道如何使用 JQuery,但是我不能使用 jQuery 或任何其他类似的库)

【问题讨论】:

  • 可以使用普通的DOM,即document.getElementById('color'),还是必须使用css-selectors?
  • 两个都可以用,有关系吗?

标签: javascript html css-selectors


【解决方案1】:

selectedOptions 是一个有效的选项,用于从选择元素中取回选定的选项

文档中的演示:

let orderButton = document.getElementById("order");
let itemList = document.getElementById("foods");
let outputBox = document.getElementById("output");

orderButton.addEventListener("click", function() {
  let collection = itemList.selectedOptions; // <-- used here
  let output = "";

  for (let i=0; i<collection.length; i++) {
    if (output === "") {
      output = "Your order for the following items has been placed: ";
    }
    output += collection[i].label;

    if (i === (collection.length - 2) && (collection.length < 3)) {
      output +=  " and ";
    } else if (i < (collection.length - 2)) {
      output += ", ";
    } else if (i === (collection.length - 2)) {
      output += ", and ";
    }
  }

  if (output === "") {
    output = "You didn't order anything!";
  }

  outputBox.innerHTML = output;
}, false);
<label for="foods">What do you want to eat?</label><br>
<select id="foods" name="foods" size="7" multiple>
  <option value="1">Burrito</option>
  <option value="2">Cheeseburger</option>
  <option value="3">Double Bacon Burger Supreme</option>
  <option value="4">Pepperoni Pizza</option>
  <option value="5">Taco</option>
</select>
<br>
<button name="order" id="order">
  Order Now
</button>
<p id="output">
</p>

【讨论】:

    【解决方案2】:

    你可以用querySelectorAll而不是querySelector来做到这一点

    document.querySelectorAll('option:checked')[0].innerText
    

    或

    document.querySelectorAll('option:checked')[0].value
    

    【讨论】:

      【解决方案3】:

      使用 :checked 选择器。这适用于复选框、单选和选择

      document.querySelector('#color option:checked')
      

      对于节点

      document.querySelector('#color option:checked').value
      

      为了价值

      【讨论】:

      • 这个答案是最好的。我得到了以下内容: ("#color").selectedOptions[0].value 但它不像这个答案那么干净。
      【解决方案4】:

      这将返回选定的选项值和文本。希望这对你有用..

      干杯

      var elt = document.getElementById('color');
      
       // get option selected
          var option = elt.options[elt.selectedIndex].value;
          var optionText = elt.options[elt.selectedIndex].text;
      

      【讨论】:

        【解决方案5】:

        使用getElementById() 获取&lt;select&gt; DOM 元素并获取其参数selectedIndex:

        var select = document.getElementById( 'color' ),
            selIndex = select.selectedIndex;,
            selElement = select.getElementsByTagName( 'option' )[ selIndex ];
        

        【讨论】:

          【解决方案6】:

          在纯javascript中:

          var select = document.getElementById('color');
          var currentOpt = select.options[select.selectedIndex]; 
          

          JsBin 示例:http://jsbin.com/ogunet/1/edit(打开你的 js 控制台)

          【讨论】:

          • 我想 Guy 想知道是否有办法使用 css-selectors 来做到这一点。
          猜你喜欢
          • 2015-01-30
          • 1970-01-01
          • 2017-09-12
          • 2011-08-10
          • 1970-01-01
          • 1970-01-01
          • 2011-10-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多