我试图使用ScrollectBox插件实现克隆选择菜单:
https://github.com/afEkenholm/ScrollectBox/blob/master/index.html
https://github.com/afEkenholm/ScrollectBox/blob/master/js/ScrollectBox/jquery.scrollectbox.js
但是我无法获得选择菜单的选项值。它将返回选项文本。
如何使用onChange
调用函数在以下选择菜单jQuery中获得选项的值(而不是文本)?
<script type="text/javascript">
jQuery(document).ready(function($){
$(".selection").scrollectBox({
preset: 'dropdown',
numVisibleOptions: 4,
scrollInterval: 150,
scrollOn: 'hover'
});
});
</script>
<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>
不工作
<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>
它返回[object Object]
不工作
<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>
它返回[object Object]
发布于 2013-03-21 11:52:07
根据ScrollectBox的源代码,您必须使用onSelectEvent
属性,如下所示:
jQuery(document).ready(function($){
$(".selection").scrollectBox({
preset: 'dropdown',
numVisibleOptions: 4,
scrollInterval: 150,
scrollOn: 'hover',
onSelectEvent: function (item, event) {
alert(item.val());
}
});
});
发布于 2013-03-21 11:52:20
您可以通过:$("#selector option:selected")
引用所选选项
$("#selector").change(function() {
var selectedValue = $("#selector option:selected").val();
alert('Selected value ' + selectedValue);
});
JS Fiddle: http://jsfiddle.net/Y7byM/1/
编辑
您可以从您的评论中将#selector
更改为.selector
$(".selection").change(function() {
var selectedValue = $(".selector option:selected").val();
alert('Selected value ' + selectedValue);
});
发布于 2013-03-21 11:53:17
试过这个吗?
$('#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);
});
https://stackoverflow.com/questions/15546670
复制相似问题