多选 [英] multiple select

查看:92
本文介绍了多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要在下拉框中获取所有选中的值。请参阅示例。

 < html> ; 
< head>
< script>
函数getSelected()
{
alert(document.myform.mytextarea.value);
返回false;
}
< / script>
< title>< / title>
< / head>
< body>
<表格名称= myform>
< select id = mytextarea size = 3 multiple>
< option id = one value = one>一个< / option>
< option id = two value = two>两个< / option>
< option id = three value = three>三个< / option>
< option id = four value = four>四个< / option>
< / select>
< input type =buttononclick =getSelected();/>
< / form>
< / body>
< / html>

如何检索在下拉列表中选择的所有多个值.Rightnow我只能获得一个值您可以使用类似于此的

 <$ c   $ c> function getSelected()
{
var dropDownElem = document.getElementById(mytextarea);
var selectedValues = new Array();
var dropDownLength = dropDownElem.length;

for(var i = 0; i< dropDownLength; i ++)
{
if(dropDownElem.options [i] .selected)
{
selectedValues.push(dropDownElem.options [i] .value);
}
}

alert(selectedValues.toString()); //获取由','分隔的值
alert(selectedValues.join(';')); //获取由';'
}

注意

从HTML端移动你的javascript也是一个好习惯。从HTML中删除onclick处理程序,并将其绑定到< script> 标记中。


hi I need to get all the values selected in a drop down box.Please see the example.

<html>
<head>
<script>
function getSelected()
{
alert(document.myform.mytextarea.value);
return false;
}
</script>
<title></title>
</head>
<body>
<form name=myform>
<select id=mytextarea size=3 multiple>
<option id=one value=one> one </option>
<option id=two value=two> two </option>
<option id=three value=three> three </option>
<option id=four value=four> four </option>
</select>
<input type="button" onclick="getSelected();"/>
</form>
</body>
</html>

How to retrieve all the multiple values selected in the dropdown.Rightnow I am getting only one value

解决方案

You can use something like this

function getSelected()
{
    var dropDownElem = document.getElementById ( "mytextarea" );
    var selectedValues = new Array();
    var dropDownLength = dropDownElem.length;

    for ( var i=0; i < dropDownLength; i++ )
    {
        if ( dropDownElem.options[i].selected )
        {
            selectedValues.push ( dropDownElem.options[i].value );
        }
    }

    alert ( selectedValues.toString() ); // gets the values separated by ','
    alert ( selectedValues.join(';') ); // gets the values separated by ';'
}

Note

Also a good practice to move your javascript from HTML side. Remove you onclick handler from HTML and bind that inside your <script> tag.

这篇关于多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆