html select - Get selected value in dropdown list using JavaScript

Mots clés : javascripthtml-selectjavascript

meilleur 5 Réponses html select - Get selected value in dropdown list using JavaScript

vote vote

93

<select id="ddlViewBy">   <option value="1">test1</option>   <option value="2" selected="selected">test2</option>   <option value="3">test3</option> </select> 
var e = document.getElementById("ddlViewBy"); var strUser = e.value; 
var e = document.getElementById("ddlViewBy"); var strUser = e.options[e.selectedIndex].text; 
vote vote

88

var e = document.getElementById("elementId"); var value = e.options[e.selectedIndex].value; var text = e.options[e.selectedIndex].text; 
$("#elementId :selected").text(); // The text content of the selected option $("#elementId :selected").val(); // The value of the selected option 
// HTML <select ng-model="selectItem" ng-options="item as item.text for item in items"> </select> <p>Text: {{selectItem.text}}</p> <p>Value: {{selectItem.value}}</p>  // JavaScript $scope.items = [{   value: 'item_1_id',   text: 'Item 1' }, {   value: 'item_2_id',   text: 'Item 2' }]; 
vote vote

78

var strUser = e.options[e.selectedIndex].value; 
var strUser = e.options[e.selectedIndex].text; 
<select>     <option value="hello">Hello World</option> </select> 
vote vote

64

 <select id="Ultra" onchange="run()">  <!--Call run() function-->      <option value="0">Select</option>      <option value="8">text1</option>      <option value="5">text2</option>      <option value="4">text3</option> </select><br><br> TextBox1<br> <input type="text" id="srt" placeholder="get value on option select"><br> TextBox2<br> <input type="text" id="rtt"  placeholder="Write Something !" onkeyup="up()"> 
<script>     function run() {         document.getElementById("srt").value = document.getElementById("Ultra").value;     } </script> 
<script>     function up() {         //if (document.getElementById("srt").value != "") {             var dop = document.getElementById("srt").value;         //}         alert(dop);     } </script> 
<script>     function up() {         var dop = document.getElementById("srt").value;         pop(dop); // Calling function pop     }      function pop(val) {         alert(val);     }? </script> 
vote vote

60

var selectedValue = document.getElementById("ddlViewBy").value; 

Questions similaires