简体   繁体   English

获取下拉列表的选定值

[英]Get the selected value of a dropdown list

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(). 我想在用户更改下拉列表时获取下拉列表中所选值的值,或者希望默认值由alert()显示。 I need to use alert() from within the < body > of the html. 我需要从html的<body>中使用alert()。 Is that possible? 那可能吗?

<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>

Use the code: 使用代码:

selectElement.options[ selectElement.options.selectedIndex ];

to find the index that is selected. 查找选定的索引。

So where you say var drp = document.getElementById("numberlist"); 所以说var drp = document.getElementById("numberlist"); you can now find the value of the index by doing: 您现在可以通过执行以下操作找到索引的值:

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

Add the code into an onchange event so that whenever you change the selectedIndex of the element, you will be able to obtain it's value: 将代码添加到onchange事件中,以便每当更改元素的selectedIndex时,都可以获取其值:

Here is a DEMO 这是一个演示

add this function within script tag. 在script标签内添加此功能。

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

and add function call like this. 并添加这样的函数调用。

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

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>

Here is a JQuery method: 这是一个JQuery方法:

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

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

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