按钮单击不会触发JQuery函数

时间:2016-10-24 17:50:00

标签: jquery html forms asp.net-mvc-4

我的控制器有动作结果: -

    [HttpPost]
    [LogInFilter]
    public JsonResult MemberCreate(GarhwalBhawan.Models.MemberTbl Mem) // Record Insert  
    {
        if (ModelState.IsValid)
        {
            db.MemberTbls.Add(Mem);
            db.SaveChanges();
            return Json(Mem, JsonRequestBehavior.AllowGet);
        }
        return Json(Mem, JsonRequestBehavior.AllowGet);
    }    

    [HttpGet]
    [LogInFilter]
    public PartialViewResult MemberCreate()   //Insert PartialView  
    {
        return PartialView(new GarhwalBhawan.Models.MemberTbl());
    }

在我的视图中我创建了一个表单并在按钮上单击我将json函数调用为: -



 <fieldset>
        <legend></legend>

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

        <div class="editor-label">
            Member Post
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Post)
            @Html.ValidationMessageFor(model => model.Post)
        </div>

        <div class="editor-label">
            Member Phone
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Phone)
            @Html.ValidationMessageFor(model => model.Phone)
        </div>

        <p>
            <input id="memberSave" type="button" value="Create"  />
        </p>
    </fieldset>
}
&#13;
&#13;
&#13;

和MemberSave的脚本函数为: -

&#13;
&#13;
@section Scripts {

<script type="text/javascript">

   $(document).ready(function() {
  $("#memberSave").click(function()  {
        var Name = $("#Name").val();
        var Post = $("#Post").val();
        var Phone = $("#Phone").val();

        var Member = {
            "Name": Name, "Post": Post,
            "Phone": Phone
        });

        $.post("/Admin/MemberCreate", Member,
        function (data) { if (data == 0) { location = location.href; } }, 'json');
    });
</script>
}
&#13;
&#13;
&#13;

问题是点击“创建”按钮后,它没有触发任何JQuery的点击事件。

请帮助。

3 个答案:

答案 0 :(得分:0)

好吧,你把它标记为jquery,为什么不使用jquery附加处理程序?

<input type="button" id="member_save" value="Create"  />

<script>


$(document).ready(function(){ 
    $('#member_save').on('click', function(){ 
       //your code here
     });

});

</script>

答案 1 :(得分:0)

只需添加id到按钮,然后以这种方式单击该id的事件。

&#13;
&#13;
$(document).ready(function() {
      $("#myButton").click(function() {
        alert("button clicked.");
        //do ajax post here
      });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="myButton" type="button" value="Create" />
&#13;
&#13;
&#13;

答案 2 :(得分:0)

请正确关闭括号..

    @section Scripts {

    <script type="text/javascript">

       $(document).ready(function() {
      $("#myButton").click(function()  {
            var Name = $("#Name").val();
            var Post = $("#Post").val();
            var Phone = $("#Phone").val();

            var Member = {
                "Name": Name, "Post": Post,
                "Phone": Phone
            };)};

            $.post("/Admin/MemberCreate", Member,
            function (data) { if (data == 0) { location = location.href; } }, 'json');
});
</script>
    }