从 jQuery/jQuery Mobile 的选择菜单中获取所选项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10613346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get selected item from select menu in jQuery/jQuery Mobile?
提问by gurehbgui
I have a jQuery Mobile code like this:
我有一个像这样的 jQuery Mobile 代码:
<select name="selectmenu1" id="selectmenu1">
<option value="o1">
a
</option>
<option value="o2">
b
</option>
<option value="o3">
c
</option>
<option value="o4">
d
</option>
</select>
Now I want to get the selected value after the user changed the selection.
现在我想在用户更改选择后获取所选值。
How to do this with jQuery?
如何用 jQuery 做到这一点?
回答by alex
It's very simple...
这很简单...
$("#selectmenu1").change(function() {
var selected = $(this).val(); // or this.value
});
If you wanted the selected element's text node(s), use this instead...
如果您想要所选元素的文本节点,请改用它...
$("#selectmenu1").change(function() {
var selected = $(this).find(":selected").text();
});?
回答by Pavnish Yadav
Here is the right answer for Jquery Mobile device .
这是 Jquery Mobile device 的正确答案。
$(document).bind( "change",'#selectmenu1', function(event, ui) {
alert($( '#selectmenu1' ).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<select name="selectmenu1" id="selectmenu1">
<option value="o1">
a
</option>
<option value="o2">
b
</option>
<option value="o3">
c
</option>
<option value="o4">
d
</option>
</select>
回答by Engineer
$('#selectmenu1').on('change', function(){
alert( this.value );
});
回答by Code Spy
http://jsfiddle.net/ipsjolly/5RaDB/
http://jsfiddle.net/ipsjolly/5RaDB/
<select name="selectmenu1" id="selectmenu1">
<option value="o1">
a
</option>
<option value="o2">
b
</option>
<option value="o3">
c
</option>
<option value="o4">
d
</option>
</select>
<div id="getval" ></div>
<script>
function displayVals() {
var singleValues = $("select").val();
$("#getval").html(singleValues);
}
$('#selectmenu1').change(function() {
displayVals();
});?
</script>
回答by Owais Iqbal
try this.. this is the simplest way..
试试这个..这是最简单的方法..
<select name="selectmenu1" id="selectmenu1" onchange="selectvalue(this.value)>
<option value="o1">
a
</option>
<option value="o2">
b
</option>
<option value="o3">
c
</option>
<option value="o4">
d
</option>
</select>
On JavaScript side code is ..
在 JavaScript 端代码是 ..
function selectvalue(value){
alert(value);
}