简体   繁体   中英

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.

With jQuery you could do $("#list option:selected").text(); to get the text of the selected option $("#list option:selected").val(); to get the value behind the selected option

EDIT:

where #list is the id of your select tag

我认为以下解决方案更好地获取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>
<body>

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

</body>
</html>

​Use this jQuery Code:

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

Here's a live example: http://jsfiddle.net/hesher/3M36e/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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