簡體   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