从5个组合框中获取选定的值

时间:2017-06-29 07:34:36

标签: javascript drop-down-menu combobox

我有3个下降,但它可以改变。它是动态下拉

image

如何在这些下拉列表中获取所有选定的值?

1 个答案:

答案 0 :(得分:2)

这是我的评论的解释.. 运行代码段以检查其运行情况

var combos = document.querySelectorAll('[name^="select"]');

function getvalues() {
  
  // Clear shown data
  console.clear();

  for (var i=0; i < combos.length; i++) {
    // Get selected option value
    var option = combos[i].options[combos[i].selectedIndex]; 
    
    // Show options
    console.log(combos[i].name + " ::  " + option.value + " : " + option.text);
  }
}
<select name="select-1">
  <option value="1" selected>First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

<select name="select-2">
  <option value="1">First</option>
  <option value="2" selected>Second</option>
  <option value="3">Third</option>
</select>

<select name="select-3">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3" selected>Third</option>
</select>

<button onclick="getvalues()">Get values</button>