简体   繁体   English

获取多选的价值 - 没有 jQuery

[英]Get value of multi-select - No jQuery

Every relevant "question that may already have [my] answer" uses jQuery, which I am not using.每个相关的“可能已经有 [我的] 答案的问题”都使用 jQuery,我没有使用它。

So, is there any simple way to get the values of selected options in a <select multiple> tag, or do I have to loop through all the options to see which ones are selected and manually build an array?那么,是否有任何简单的方法可以获取<select multiple>标记中所选选项的值,或者我是否必须遍历所有选项以查看选择了哪些选项并手动构建数组?

Side-question: Which browsers don't support selectElement.value and instead require selectElement.options[selectElement.selectedIndex].value ? selectElement.value问题:哪些浏览器支持selectElement.value而是需要selectElement.options[selectElement.selectedIndex].value

You can use select.selectedOptions .您可以使用select.selectedOptions However, this returns an HTMLCollection , so you still have to clean it to get a string array.但是,这会返回一个HTMLCollection ,因此您仍然需要清理它以获取字符串数组。 http://jsfiddle.net/9gd9v/ http://jsfiddle.net/9gd9v/

<select multiple>
  <option value="foo" selected>foo</option>
  <option value="bar">bar</option>
  <option value="baz" selected>baz</option>
</select>

and:和:

var select = document.querySelector("select");
var values = [].map.call(select.selectedOptions, function(option) {
  return option.value;
});

If you end up wanting to loop through and grab the selected values you could use something like this:如果您最终想要遍历并获取选定的值,您可以使用以下内容:

function loopSelected()
{
  var txtSelectedValuesObj = "";
  var selectedArray = new Array();
  var selObj = document.getElementById('selectID');
  var i;
  var count = 0;
  for (i=0; i<selObj.options.length; i++) {
    if (selObj.options[i].selected) {
      selectedArray[count] = selObj.options[i].value;
      count++;
    }
  }
  txtSelectedValuesObj = selectedArray;
  alert(txtSelectedValuesObj);  
}

You can view an example HERE , adapted from this example .您可以在此处查看示例,该示例改编自此示例

. .

You could also simply track the selected options via the onchange event in real-time and collect them whenever you want them.您还可以通过onchange事件实时跟踪所选选项,并在需要时收集它们。 I admit it's still looping, but at least you're not doing it every time you need to retrieve the selected options, and it has the added bonus of being simple (come retrieval time, anyway...).我承认它仍然在循环,但至少你不是每次需要检索所选选项时都这样做,而且它具有简单的额外好处(无论如何,检索时间......)。 Working fiddle: http://jsfiddle.net/cyg9Z/工作小提琴: http : //jsfiddle.net/cyg9Z/

var Test;
if (!Test) {
    Test = {
    };
}
(function () {
    Test.trackSelected = function (e) {
        var selector = document.getElementById('selector'),
            selected = [],
            i = 0;
        for (i = 0; i < selector.children.length; i += 1) {
            if (selector.children[i].selected) {
                selected.push(selector.children[i].value)
            }
        }
        selector.selMap = selected;
    };
    Test.addListeners = function () {
        var selector = document.getElementById('selector'),
            tester = document.getElementById('tester');
        selector.onchange = Test.trackSelected;
        tester.onclick = Test.testSelected;
    };
    Test.testSelected = function () {
        var div = document.createElement('div');
        div.innerText = document.getElementById('selector').selMap.join(', ');
        document.body.appendChild(div);
    };
    Test.addListeners();
}());​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM