简体   繁体   English

从下拉列表中选择的值

[英]selected value from dropdown

How to check what value is selected in dropdown html?如何检查下拉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这可能会有所帮助: 使用 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 ||但我有变量问题:nr_days || comunicate: nr_days dont exist沟通:nr_days 不存在

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

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

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

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