繁体   English   中英

使用ASP.NET MVC的jQuery按钮单击事件未按预期响应

[英]JQuery button click event doesn't respond as expected using asp.net mvc

请参阅我的_Layout.cshtml代码

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")


    </head>
    <body>
        <header>
            <div class="content-wrapper">
                <div class="float-left">
                    <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
                </div>
                <div class="float-right">
                    <section id="login">
                        @Html.Partial("_LoginPartial")
                    </section>
                    <nav>
                        <ul id="menu">
                            <li>@Html.ActionLink("Home", "Index", "Home")</li>
                            <li>@Html.ActionLink("About", "About", "Home")</li>
                            <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                        </ul>
                    </nav>
                </div>
            </div>
        </header>
        <div id="body">
            @RenderSection("featured", required: false)
            <section class="content-wrapper main-content clear-fix">
                @RenderBody()
            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
                </div>
            </div>
        </footer>

        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryui")
        @Styles.Render("~/Content/themes/base/css")
        @RenderSection("scripts", required: false)
    </body>
</html>

我检查了firebug的net选项卡,发现所有js文件都已从客户端PC下载,但仍然与jQuery相关,但没有起作用。

这是第一个显示警报,只需单击不起作用的按钮

$(function () {
        $("#submit1").click(function () {
            alert("button");
        });

         $(".datePicker").datepicker({
            showOn: "button",
            buttonImage: "/images/calendar-icon.png",
            buttonImageOnly: true,
            buttonText: "Select date"
         });
     });

当单击按钮时,警报不会显示。

日期选择器不来。

我在哪里弄错了。 请帮我整理一下。 我是否缺少要包含的文件? 谢谢

编辑

我的view.cshtml代码如下

@model MvcApplication1.Controllers.Employee


<div class="editor-label">
    @Html.LabelFor(model => model.Id)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Id)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Name)
</div>


<div class="editor-label">
    @Html.LabelFor(model => model.Salary)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Salary)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.IsMale)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.IsMale)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.JoinDate)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.JoinDate, new { @class = "datePicker" })
</div>

<script>
     $(function () {
         $(".datePicker").datepicker({
            showOn: "button",
            buttonImage: "/images/calendar-icon.png",
            buttonImageOnly: true,
            buttonText: "Select date"
         });
     });

</script>

编辑器使用元数据,请参见 http://aspadvice.com/blogs/kiran/archive/2009/11/29/Adding-html-attributes-support-for-Templates- 2D00 -ASP.Net-MVC-2.0-Beta_2D00_1 .aspx

或将@ Html.EditorFor(model => model.JoinDate,new {@class =“ datePicker”})更改为@ Html.TextBoxFor(model => model.JoinDate,new {@ class =“ datePicker”})

假设您的View.cshtml是正在@RenderBody中呈现的页面,则需要在View.cshtml中的一部分中引用脚本,如下所示:

@section Scripts {
  @Scripts.Render("~/Scripts/app/myscript.js")
}

这是JS工作的一个示例(您说的没错): https : //jsfiddle.net/emonrc9c/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM