jquery怎么获取select选中的值

如题所述

JS:  document.getElementById("sid").value;

Jquery: $("#sid").val();

直接就可以获取指定select的选中的值;

如果是多选的话,需要用其他方法。

alert($("#eID").val()+"/"+document.getElementById("eID").value);

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-04-09

jQuery取select选中的值方法如下:



例子:

<select id="myselect">    <option value="1">Mr</option>    <option value="2">Mrs</option>    <option value="3">Ms</option>    <option value="4">Dr</option>    <option value="5">Prof</option></select>

jQuery("#myselect  option:selected").text(); // => "Mr"


还有一点要注意如果 select是checkbox 要这样使用:

jQuery("#select1  option:checked ").text();



JQ 完成这个选择器的核心代码

// Loop through all the selected options    
for ( ; i < max; i++ ) {    
option = options[ i ];    
// oldIE doesn't update selected after form reset (#2551)    
if ( ( option.selected || i === index ) &&    
// Don't return options that are disabled or in a disabled optgroup    
( support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null ) &&    
( !option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" ) ) ) {    
// Get the specific value for the option    
value = jQuery( option ).val();    
// We don't need an array for one selects    
if ( one ) {    
return value;    
}    
// Multi-Selects return an array    
values.push( value );    
}    
}

本回答被网友采纳
第2个回答  2016-01-11
可以用Jquery的选择器来实现,

$("select option:selected").next()
<select>
<option value="1" selected="selected">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script>
$(function() {
$("select").change(function(event) {
var obj = $(this).find("option:selected").next();
alert("选中项的下一个: 内容"+obj.html()+",值"+obj.val());
});

})
</script>
Jquery是一个优秀的Javascript库,还兼容各种浏览器。jQuery使用户能更方便地处理HTML、events、实现动画效果,并且方便地为网站提供AJAX交互。本回答被提问者采纳
第3个回答  2016-01-15
根据下拉菜单属性选取 就好了 比如id $("#id option:selected").val()
第4个回答  2016-01-14
$("input[name="test"]:checked")