如何从javascript中的选择选项获取多个值?

时间:2018-10-01 03:02:00

标签: javascript jquery drop-down-menu select-options

我想从我的代码中获取所有数据,我只能在myFunction上获取一个。如何从javascript中的选择选项获取多个值?我在选择多个时使用了jQuery插件 来自选择选项的数据,但是在生成时无法获取所有数据。我想要全部 从选择框打印或记录的数据。但是如屏幕截图所示,有多个值,但生成时只有1个数据     生成的是#10Recovery

Interface

#select menu
<select id="mySelect2" class="chosen" multiple="true">
          {% for product in products.response.result %}
          <option value="{{ product.item_name }}">{{ product.item_name }}</option>
          {% endfor %}
</select>

#jquery plugin in getting multiple values from a menu
 </script>
 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.min.css"
 <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js"></script>
 <script>
        jQuery(document).ready(function(){
            jQuery(".chosen").data("placeholder","Select Products").chosen();
        });
 </script>



#script in getting values from select option
    <script>
    function myFunction() {
        var z = document.getElementById("mySelect2").value;
        document.getElementById("demo2").innerHTML = z;
    }

    But as on the screenshot there were multiple values but when generated only 1 data that
    was generated which is #10Recovery
    #code on generating
<a class="button" onclick="myFunction()">{% trans 'Generate' %}</a>

1 个答案:

答案 0 :(得分:1)

您可以使用jQuery。

jQuery("#mySelect2").val();