javascript 获取下拉列表的选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11244331/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:31:42  来源:igfitidea点击:

Get the selected value of a dropdown list

javascripthtmlcordova

提问by harsh

I have the follwing code. I want to get the value of the selected value of the dropdownlist once the user changes it or to have the default value to be displayed by alert(). I need to use alert() from within the < body > of the html. Is that possible?

我有以下代码。我想在用户更改下拉列表的选定值后获取它的值,或者让 alert() 显示默认值。我需要在 html 的 < body > 中使用 alert()。那可能吗?

<html>
 <head>
 <title>Title</title>
 <script type="text/javascript" src="phonegap-1.4.1.js">
</script> <script type="text/javascript" src="JS/Util.js"></script>
 <script type="text/javascript" src="JS/Language.js"></script>
 <script type="text/javascript">
   function dropdown(){
  var drp = document.getElementById("numberlist");
 var optn = document.createElement("OPTION");
 optn.text="3";
 optn.value="3";
 drp.add(optn);
 }
    </script>
 </head>
  <body onload="dropdown();"> <div id="main3"></div>  <form><select id = "numberlist"><option>12</option><option>24</option><option>36</option></select>

回答by 0x499602D2

Use the code:

使用代码:

selectElement.options[ selectElement.options.selectedIndex ];

to find the index that is selected.

找到被选中的索引。

So where you say var drp = document.getElementById("numberlist");you can now find the value of the index by doing:

所以你说var drp = document.getElementById("numberlist");你现在可以通过执行以下操作找到索引的值:

var value = drp.options[ drp.options.selectedIndex ].value;

Add the code into an onchangeevent so that whenever you change the selectedIndexof the element, you will be able to obtain it's value:

将代码添加到onchange事件中,这样无论何时更改selectedIndex元素的 ,您都可以获取它的值:

Here is a DEMO

这是一个演示

回答by Muhammad Saifuddin

add this function within script tag.

在脚本标签中添加此功能。

function pickvalue(id){
   var drp = document.getElementById(id);
   alert(drp.value);
}

and add function call like this.

并添加这样的函数调用。

<select id = "numberlist" onchange="pickvalue(this.id)">

回答by himadri

Try this Following...Mark it as answer if it helps you

试试这个以下...如果它对你有帮助,请将其标记为答案

<html>  
<head>  
<title>Title</title>  
<script type="text/javascript" src="phonegap-1.4.1.js"> </script> <script         type="text/javascript" src="JS/Util.js"></script>  
<script type="text/javascript" src="JS/Language.js"></script> 
<script type="text/javascript">    
function dropdown()
{   
    var drp = document.getElementById("numberlist");  
    var optn = document.createElement("OPTION");  
    optn.text="3";  
    optn.value="3";  
    drp.add(optn);  
}  

</script>  
</head>   
<body onload="dropdown();"> 
<div id="main3">
</div>  
<form>
<select id="numberlist" onchange="show()">
<option>12</option>
<option>24</option>
<option>36</option>
</select> 
<script type="text/javascript">
    function show() {
        var e = document.getElementById('numberlist');
        var txt = e.options[e.selectedIndex].text;
        alert(txt);


    }   
</script>
</form>
</body>
</html>

回答by Danny

Here is a JQuery method:

这是一个 JQuery 方法:

 $value = $("#numberlist option:selected").text();
 alert($value);