简体   繁体   中英

selected value from dropdown

How to check what value is selected in dropdown html? in real time (client side)

<div>
    <select id="myList" name="testList">
        <option value="1">Jan</option>
        <option value="2">Feb</option>
        <option value="3">Mar</option>
    </select>
</div>



 @{ 
    if ***[if the value from dropdown = 1) ]?***
      {
     var variable = x;
      }
 }

This might help: Get selected value in dropdown list using JavaScript

But here's an example:

 getSelected(); function getSelected() { let list = document.getElementById("myList"); let selected = list.options[list.selectedIndex].value; let writeSelected = document.getElementById("selectedItem") if (selected === "2") { return writeSelected.innerHTML = "Best month" } writeSelected.innerHTML = selected }
 <div> <select id="myList" onchange="getSelected()" name="testList"> <option value="1">Jan</option> <option value="2">Feb</option> <option value="3">Mar</option> </select> </div> <p> Selected: <span id="selectedItem"></span> </p>

I write sth this

<script>
       var XXX = document.getElementsByName("testList");
       var YYY = XXX.options[list_month.selectedIndex].value;

       var nr_days = YYY;
    </script>

but I have problem with variable: nr_days || comunicate: nr_days dont exist

  @for (int nr_rows = 0; nr_rows < nr_days; nr_rows++)

.cshtml

you can use this code to check what value is selected.

/***This code is to get value of selected option****/
$("select#myList").change(function(){
        var selectedMonth = $(this).children("option:selected").val();
        alert("You have selected the month - " + selectedMonth);
    });
/***This code is to get text of selected option****/
$("select#myList").change(function(){
        var selectedMonth = $(this).children("option:selected").text();
        alert("You have selected the month - " + selectedMonth);
    });
<html>
<head>
<script>
function demo()
{
var a=document.getElementById("txt").value;
document.write("selected month is"+a);
}
</script>
</head>
<body>
     <div>
    <select id="myList" name="testList" onchange="demo()" id="txt">
        <option value="1">Jan</option>
        <option value="2">Feb</option>
        <option value="3">Mar</option>
    </select>
        </div>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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