我遇到了一些麻烦。
我的 mvc 应用程序中有以下 View :
@model Radio_Management_Interface.Models.Blocks.blocks_create
@{
ViewBag.Title = "Create Block";
Layout = "~/Views/Shared/_Layout_main.cshtml";
}
<script>
function getSelectedValue(sel) {
window.alert("You change the combo box");
}
</script>
<div class="contenttitle">
<h2 class="form"><span>Create A New Block</span></h2>
</div><!--contenttitle-->
@using (Html.BeginForm("Create", "Blocks", FormMethod.Post, new { @class = "stdform" }))
{
@Html.AntiForgeryToken()
<hr />
@Html.ValidationSummary(true)
<p>
<label>Select Network</label>
<span class="field">
<select name="select" onclick="getSelectedValue">
@foreach (var item in Model.networks)
{
<option value="@item.networkID">@Html.DisplayFor(modelItem => item.name)</option>
}
</select>
</span>
@Html.ValidationMessageFor(model => model.block.start)
<small class="desc">Name of the customer.</small>
</p>
具体关注的部分是:
<select name="select" onclick="getSelectedValue">
为什么这个事件不会被调用?当我更改网站上 select 元素中的选项时,我的 JavaScript 文件不会运行。为什么会这样?
最佳答案
您希望使用 onchange
事件而不是 onclick
并在您的方法需要参数时向其传递一些内容:
HTML:
<select name="select" onchange="getSelectedValue(this)">
JS:
function getSelectedValue(sel) {
window.alert(document.getElementById(sel.value));
}
关于c# - javascript 未在 ASP.NET MVC 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21279718/