gpt4 book ai didi

jquery - MVC - dropdownlist onchange 从未在 jquery 中触发

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

我正在努力在 jquery 中获取下拉列表 onchange 事件。从下拉列表中选择日期时,其他文本框应不可见,而日期文本框应可见。当选择“状态”时,其他文本框应该不可见,而下拉列表的状态应该可见。什么也没有发生。请看看我的代码我做错了什么。你的帮助意义重大。

_Layout.cshtml

 <head>
<script type="text/javascript">
$(function () {
$('#categorie').on('change', function () {
if ($(this).val() == "Date") {
$('#keyword').hide(); //invisible
$('#txtcalendar').show();
} else if ($(this).val() == "Status"){
$('#keyword').hide(); //invisible
$('#txtcalendar ').hide();
$('#tmstatus ').show();
}
.
.
.
});

});
</script>
</head>

Index.cshtml

@Html.DropDownList("categorie", new SelectList(new[]
{
"All", "Id", "Status",
"Vendor", "Date"
}) as SelectList)



<p> Keyword: @Html.TextBox("keyword") <input id="Submit" type="submit" value="Search" /> </p>

<p>Calendar @Html.TextBox("txtcalendar", new {}, new { @class = "myclass", style = "display:none;" })</p>

@Html.DropDownList("tmstatus", new SelectList(new[]
{
"Success", "Pending", "Error",
}) as SelectList)

最佳答案

在您的应用程序中,转到 app_start\BundleConfig.cs 并在 RegisterBundles 方法中检查 Jquery 的捆绑是否已注册。它应该添加 jquery 包,如下所述:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js"));

如果 RegisterBundles 方法中不存在,则添加它。并执行以下步骤:

  1. 转到您的_Layout.cshtml页面

  2. 在 head 标签末尾添加 @Scripts.Render("~/bundles/jquery")。在我的示例应用程序中,它如下所示:

    enter image description here

  3. _Layout.cshtml 中删除下拉列表更改的 javascript 代码

  4. 将其粘贴到 index.cshtml末尾中。

我的Index.cshtml页面如下所示:

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index Page</h2>

@Html.DropDownList("categorie", new SelectList(new[]
{
"All", "Id", "Status",
"Vendor", "Date"
}) as SelectList)



<p> Keyword: @Html.TextBox("keyword") <input id="Submit" type="submit" value="Search" /> </p>

<p>Calendar @Html.TextBox("txtcalendar", new { }, new { @class = "myclass", style = "display:none;" })</p>

@Html.DropDownList("tmstatus", new SelectList(new[]
{
"Success", "Pending", "Error",
}) as SelectList)

<script type="text/javascript">
$(function () {
$('#categorie').on('change', function () {
if ($(this).val() == "Date") {
$('#tmstatus ').show();
$('#keyword').hide(); //invisible
$('#txtcalendar').show();
} else if ($(this).val() == "Status") {
$('#keyword').hide(); //invisible
$('#txtcalendar ').hide();
$('#tmstatus ').show();
}
});

});
</script>

关于jquery - MVC - dropdownlist onchange 从未在 jquery 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21572792/

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