假设我有一个像这样的视图 Foo.cshtml:
<form method="post" action="/">
<select id="Country" name="Country">
<option value="MX">Mexico</option>
<option value="CA">Canada</option>
<option value="US">USA</option>
</select>
<button type="submit">Register</button>
</form>
@{
string selectedValue = ???;
string selectedText = ???;
}
我怎么能去初始化这些字符串?
我认为
下面的代码行中的服务器端和客户端存在误解,由服务器呈现和释义
@{
string selectedValue = ???;
string selectedText = ???;
}
这意味着当它到达浏览器时,它完成并且无法更改
如果您想在下拉列表中选择值,您应该使用 javascript 来获取您想要的而不是服务器端语言
你可以使用javascript
var e = document.getElementById("Country");
var value= e.options[e.selectedIndex].value;
var text= e.options[e.selectedIndex].text;
或通过 jquery 获取
$('#Country :selected').text();
$('#Country :selected').val();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句