简体   繁体   English

Javascript选择多个选择列表

[英]Javascript to select multiple select list

I have following select list from which user can select multiple option 我有以下选择列表,用户可以从中选择多个选项

<select size="6" onblur="validatevalumethod()" multiple="" id="valumethod[]" name="valumethod[]"> 
        <option value="0">Select Asset</option>

        <option value="OC">ORIGINAL COST</option>
        <option value="OCUIP">ORIGINAL COST USING INDEXED PRICES</option>
        <option value="RC">REPLACEMENT COST</option>
        <option value="OCCR">ORIGINAL COST CONSIDERING REVALUATION</option>
        <option value="OCUIPCR">ORIGINAL COST USING INDEXED PRICES & CONSIDERING REVALUATION</option>
        <option value="RCCR">REPLACEMENT COST CONSIDERING REVALUATION</option>
        </select>

I need to fetch the value selected by user in javascript but dont know how to do this please help me. 我需要获取用户在javascript中选择的值,但不知道该怎么做,请帮助我。

With jQuery you could do $("#list option:selected").text(); 使用jQuery,您可以执行$("#list option:selected").text(); to get the text of the selected option $("#list option:selected").val(); 获取所选选项$("#list option:selected").val();的文本$("#list option:selected").val(); to get the value behind the selected option 获得所选选项背后的价值

EDIT: 编辑:

where #list is the id of your select tag 其中#list是您的选择标签的ID

我认为以下解决方案更好地获取VALUES:

$("#list option:selected").val();

Here's a very simple code that tells you how many you selected on every change: 这是一个非常简单的代码,告诉您每次更改选择了多少个:

For the Html Code: 对于HTML代码:

<html>
<body>

<select multiple="true">
  <option>Volvo</option>
  <option>Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>

</body>
</html>

​Use this jQuery Code: 使用以下jQuery代码:

$("select").change(function() {
    alert($("option:selected").length);
});

Here's a live example: http://jsfiddle.net/hesher/3M36e/ 这是一个实时示例: http : //jsfiddle.net/hesher/3M36e/

您可以使用纯JavaScript,例如:document.getElementById('valumethod []')。value

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

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