简体   繁体   English

如何使用jquery获取选择菜单onChange中的选项值?

[英]how to get the value of an option in select menu onChange using jquery?

i'm trying to implement clone select menu using the following plugin: 我正在尝试使用以下插件实现克隆选择菜单:

https://github.com/afEkenholm/ScrollectBox https://github.com/afEkenholm/ScrollectBox

https://github.com/afEkenholm/ScrollectBox/blob/master/index.html https://github.com/afEkenholm/ScrollectBox/blob/master/index.html

https://github.com/afEkenholm/ScrollectBox/blob/master/js/ScrollectBox/jquery.scrollectbox.js https://github.com/afEkenholm/ScrollectBox/blob/master/js/ScrollectBox/jquery.scrollectbox.js

but i'm unable to get the option value of select menu. 但是我无法获得选择菜单的选项值。 it returns option text instead. 它返回选项文本。

how to get the value (but not text) of an option in the following select menu onChange using jquery call function? 如何使用jquery调用函数获取以下选择菜单onChange选项的值(但不是文本)?

<script type="text/javascript">
jQuery(document).ready(function($){ 
   $(".selection").scrollectBox({
    preset: 'dropdown',
    numVisibleOptions: 4,
    scrollInterval: 150, 
    scrollOn: 'hover'
    });
});

    <select onchange="function(this);" id="selector" class="selection" >
    <option value="" selected="Select Topic">Select Topic</option> 
    <option value="Food">Food</option>
    <option value="Drink">Drink</option>
    </select>

thanks, 谢谢,


RE-EDIT: 重新编辑:

Doesn't work 不行

<script type="text/javascript">
jQuery(document).ready(function($){ 
   var selectEvent = function($el){
someFunction($(this).val());
return false;
};
   $(".selection").scrollectBox({
    preset: 'dropdown',
    numVisibleOptions: 4,
    onSelectEvent: selectEvent,
    scrollInterval: 150, 
    scrollOn: 'hover'
    });
});
</script>

it returns [object Object] 它返回[object Object]

Doesn't work 不行

    <script type="text/javascript">
jQuery(document).ready(function($){ 
    $(".selection").scrollectBox({
        preset: 'dropdown',
        numVisibleOptions: 4,
        scrollInterval: 150, 
        scrollOn: 'hover',
        onSelectEvent: function (item, event) {
           alert(item).val();
        }
    });
});
</script>

it returns [object Object] 它返回[object Object]

According to the source code for ScrollectBox , you must use the onSelectEvent property, like so: 根据ScrollectBox的源代码,您必须使用onSelectEvent属性,如下所示:

jQuery(document).ready(function($){ 
    $(".selection").scrollectBox({
        preset: 'dropdown',
        numVisibleOptions: 4,
        scrollInterval: 150, 
        scrollOn: 'hover',
        onSelectEvent: function (item, event) {
           alert(item.val());
        }
    });
});

You can refer to the selected option via: $("#selector option:selected") 您可以通过以下方式参考所选选项: $("#selector option:selected")

$("#selector").change(function() {
   var selectedValue = $("#selector option:selected").val();
   alert('Selected value ' + selectedValue);
});

JS Fiddle: http://jsfiddle.net/Y7byM/1/ JS小提琴: http //jsfiddle.net/Y7byM/1/

Edit : 编辑

From your comments you can change #selector to .selector : 根据您的评论,您可以将#selector更改为.selector

$(".selection").change(function() {
   var selectedValue = $(".selector option:selected").val();
   alert('Selected value ' + selectedValue);
});

Tried this ? 试过这个?

$('#selector').change(function() {
           // assign the value to a variable, so you can test to see if it is working.
            var selectVal = $('#selector :selected').val();
            alert(selectVal);
        });

My "select" is provided via an AJAX call, and I could not get the calling program to work with the .change method. 我的“select”是通过AJAX调用提供的,我无法让调用程序使用.change方法。

The table (not declared until an AJAX call is made) is 该表(在进行AJAX调用之前未声明)是

<select id="provincePick">
<option value="0" >Choose a province:</option>
<option value="AB">Alberta</option>
<option value="BC">British Columbia</option>
<option value="MB">Manitoba</option>
<option value="NB">New Brunswick</option>
<option value="NL">Newfoundland and Labrador</option>
<option value="NS">Nova Scotia</option>
<option value="ON">Ontario</option>
<option value="PE">Prince Edward Island</option>
<option value="QC">Quebec</option>
<option value="SK">Saskatchewan</option>
<option value="NT">Northwest Territories</option>
<option value="NU">Nunavut</option>
<option value="YT">Yukon</option>

The jQuery code I needed in the calling program was 我在调用程序中需要的jQuery代码是

    $("body").on("change", "#provincePick", function(e)
    {
        alert( this.value );
    });

Now the alert works. 现在警报有效。

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

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