简体   繁体   English

<select>标记并将多个值返回给javascript方法

[英]<select> tag and returning multiple values to javascript method

I have an issue with the data which is sent from a drop down menu, the selector only returns a single value, even when multiple values are selected.我对从下拉菜单发送的数据有疑问,即使选择了多个值,选择器也只返回一个值。 I have searched online for a solution to this, but they all use PHP, JQuery or some method outside the scope of the course I am taking;我已经在网上搜索了一个解决方案,但他们都使用 PHP、JQuery 或我正在学习的课程范围之外的一些方法; to capture multiple selected items.捕获多个选定的项目。 I have tried .value of the individual options, but that returns all of the options rather than just the ones which are selected.我已经尝试了各个选项的 .value ,但这会返回所有选项,而不仅仅是选择的选项。 Is there some kind of trick to sending multiple values?发送多个值有什么技巧吗?

Here is my code for the menu.这是我的菜单代码。 For example If I select JAVA PROGRAMMING, NETWORKS and VIDEO GAMES, only JAVA PROGRAMMING is sent.例如,如果我选择 JAVA PROGRAMMING、NETWORKS 和 VIDEO GAMES,则只发送 JAVA PROGRAMMING。

<select multiple id="CK_Expertise">
  <option id="CK_Exp1" value="Java programming">JAVA PROGRAMMING</option>
  <option  id="CK_Exp2" value="Networks">NETWORKS</option>
  <option  id="CK_Exp3" value="Video game programming">VIDEO GAMES</option>
  <option  id="CK_Exp4" value="Accounter">ACCOUNTER</option>
  <option  id="CK_Exp5"  value="Help Desk">HELPDESK</option>
  <option  id="CK_Exp6"  value="C++ programming">C++</option>
  <option  id="CK_Exp7"  value="Programming">PROGRAMMING</option>
</select>

I have also tried using the Select Object in the DOM, http://www.w3schools.com/jsref/dom_obj_select.asp which has a few methods for accessing the options in the dropdown menu.我还尝试在 DOM 中使用 Select Object, http://www.w3schools.com/jsref/dom_obj_select.asp ,它有一些方法可以访问下拉菜单中的选项。 One method in particular called selectedIndex, seemed to be what I am looking for, however it only returns the the index of the first selected option, instead of all of the selected options.一种特别称为 selectedIndex 的方法似乎是我正在寻找的,但是它只返回第一个选定选项的索引,而不是所有选定选项。
Is there a simple solution to this using just Javascript and the DOM?是否有一个仅使用 Javascript 和 DOM 的简单解决方案?

Thanks - Chris谢谢 - 克里斯

Get the options, iterate and check if they are selected, and add the values to an array获取选项,迭代并检查它们是否被选中,并将值添加到数组中

var select = document.getElementById('CK_Expertise'),
   options = select.getElementsByTagName('option'),
   values  = [];

    for (var i=options.length; i--;) {
        if (options[i].selected) values.push(options[i].value)
    }

    console.log(values)

FIDDLE小提琴

or being a little more fancy或者更花哨一点

var select = document.getElementById('CK_Expertise'),
    values = Array.prototype.filter.call(select.options, function(el) {
        return el.selected;
    }).map(function(el) {
        return el.value;
    });

    console.log(values)

FIDDLE小提琴

You could use the select .您可以使用select . selectedOptions property: selectedOptions属性:

select.onchange = function() {
  var values = [].map.call(this.selectedOptions, function(opt){
    return opt.value;
  });
};

 document.getElementById('CK_Expertise').onchange = function() { document.querySelector('pre').textContent = JSON.stringify([].map.call( this.selectedOptions, function(opt){ return opt.value; } )); }
 <select multiple id="CK_Expertise"> <option id="CK_Exp1" value="Java programming">JAVA PROGRAMMING</option> <option id="CK_Exp2" value="Networks">NETWORKS</option> <option id="CK_Exp3" value="Video game programming">VIDEO GAMES</option> <option id="CK_Exp4" value="Accounter">ACCOUNTER</option> <option id="CK_Exp5" value="Help Desk">HELPDESK</option> <option id="CK_Exp6" value="C++ programming">C++</option> <option id="CK_Exp7" value="Programming">PROGRAMMING</option> </select> <pre></pre>

If you can use jQuery, this will give you all the values如果您可以使用 jQuery,这将为您提供所有值

 $(document).ready(function(){
    $('#CK_Expertise').change(function(e){
        var values = $('#CK_Expertise').val()
        alert(values);
    });
});

HTH, -Ted HTH,-泰德

You could iterate storing select.selectedIndex in an array and unselecting the corresponding option to get the next one:您可以迭代将select.selectedIndex存储在数组中并取消选择相应的选项以获取下一个选项:

select.onchange = function() {
    var i, indices=[], values = [];
    while((i=this.selectedIndex) > -1) {
        indices.push(i);
        values.push(this.value);
        this.options[i].selected = false;
    }
    while((i=indices.pop()) > -1)
        this.options[i].selected = true;
    console.log(values);
}

Demo演示

This way you avoid iterating over all options, but you must iterate twice over the selected ones (first to unselect them, them to select them again).这样可以避免遍历所有选项,但必须对选定的选项进行两次迭代(首先取消选择它们,然后再次选择它们)。

Why not using an indexed variable in the SELECT command?为什么不在 SELECT 命令中使用索引变量?

<SELECT MULTIPLE id="stuff" name="stuff[]">
<OPTION value=1>First stuff</option>
<OPTION value=2>Second stuff</option>
<OPTION value=3>Third stuff</option>
</SELECT>

In that case it's easy to read the array:在这种情况下,很容易读取数组:

$out=$_REQUEST['stuff'];
foreach($out AS $thing) {
    echo '<br />'.$thing;
}

Sorry for the poor indentation, but I just wanted to show the way I use for solving this case!抱歉缩进不佳,但我只是想展示我用于解决此案例的方式!

var select = document.getElementById('CK_Expertise'),
  options = select.selectedOptions,
  values = [];

for(let i=0;i<options.length;i++)
{
  values.push(options[i].value);
}
console.log(values);

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

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