简体   繁体   English

在javascript / jquery中获取选择选项菜单

[英]Get select option menu in javascript/jquery

I have a select option in a form like this 我在这样的表格中有一个选择选项

<select id="name" name="name" onblur="date()">
    <option value="RAN">RAN</option>
    <option value="REE">REE</option>
    <option value="SAM">SAM</option>
    <option value="SEJAl">SEJAl</option>
</select>

I want to get the value of the selected item in the list when the user looses focus. 我想在用户失去焦点时获取列表中所选项目的值。 I tried the following script; 我尝试了以下脚本;

function date(){
    var e = document.getElementById("name");    
    var name = e.options[e.selectedIndex].value; 
    console.log(name);
    //do something else....  
}

the console is empty. 控制台是空的。 A simple document.getElementById also doesnt give me the value. 一个简单的document.getElementById也没有给我这个值。 so by which method can I get the value? 那么我可以通过哪种方法获得价值?

thanks! 谢谢!

ANSWER FOUND: 答案发现:

$name = $('select[name=name]').val();

Use e.value, like this: 使用e.value,如下所示:

function date(){
    var e = document.getElementById("name");    
    console.log(e.value); 
}

JSBin JSBin

function date(){
    var e = document.getElementById("name");    
    alert(e.value); 
}

This will shows value in alert perfectly, when tou select any value ant clicks outside :) 这将显示alert中的值,当tou选择任何值蚂蚁点击外:)

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

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