我有麻烦了。

我的mvc应用程序中具有以下视图:

@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# - Java脚本未在ASP.NET MVC中调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21279718/

10-12 14:14
查看更多