gpt4 book ai didi

javascript - 为什么我的 DropDownList 更改事件无法工作?

转载 作者:行者123 更新时间:2023-12-03 10:15:16 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的 DropDownList(在 C# 中简单而有趣,但在这种情况下有点令人沮丧),以允许我在我的 MVC 应用程序中导航。

我对 JavaScript 完全没有经验,但我花了一天的大部分时间尝试各种事情,并使用 Chrome 的 JavaScript 控制台来尝试排除故障。

这就是我总结的。

Razor 引擎 View :

<h2>Index</h2>

<p>
@Html.DropDownList("DDLRegion", new SelectList(ViewBag.Tables), new { @id = "DDLRegion" }) <br />
@Html.ActionLink("Area1", "Index", "Area1") <br />
@Html.ActionLink("Area2", "Index", "Area2") <br />
@Html.ActionLink("Area3", "Index", "Area3") <br />
</p>

@section Scripts{
<script src="/Scripts/jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript">
$("#DDLRegion").change(function () {
var selectedItem = $(this).val();

switch (selectedItem) {
case '0':
window.location.href = @Url.Action("Index", "Area1");
break;
case '1' :
window.location.href = @Url.Action("Index", "Area2");
break;
case '2' :
window.location.href = @Url.Action("Index", "Area3");
break;
default:
window.location.href = @Url.Action("Index", "Area1");
alert("Blarg");
break;
}
});
</script>
}

JavaScript 控制台的最新错误是提示 @Url.Action 方法的 case '0' block 上缺少/(翻译为最终为 '/Area1')。

最佳答案

window.location.href = @Url.Action("Index", "Area1"); 这将呈现为 window.location.href=sitePrefix/Area1/Index;

现在,window.location.href=sitePrefix/Area1/Index; 不是有效的 JavaScript 语句。表达式的 RHS 应该是一个合适的值。由于这是一个 URL,因此您可以将其视为字符串。

window.location.href = '@Url.Action("Index", "Area1")'; 在 RHS 周围添加单引号即可。

关于javascript - 为什么我的 DropDownList 更改事件无法工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29910350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com