没发火两次

时间:2017-02-17 09:15:12

标签: jquery ajax asp.net-mvc

目标:
当您按下按钮时,文本框中的值应显示在公式

之下一次

问题:
当我点击按钮时,数据将显示两次。

我在下面尝试了这个code,但它不起作用

  $('#myform').one('submit', clickHandler);

  var clickHandler = function (event) {

      event.preventDefault();

  $("#divProcessing").show();

  $.ajax({
      url: $(this).attr("action"),
      type: 'POST',
      async: true,
      data: $(this).serialize(),
      dataType: 'json',
      success: function (resp) {
          // Hide the "busy" gif:
          $("#divProcessing").hide();

          // Do something useful with the data:
          $("<h3>" + resp.FirstName + " " + resp.LastName + "</h3>").appendTo("#divResult");
      }
  });

}

的信息:
*使用ASP.net mvc和JQuery
*此代码的基础来自此page

namespace WebApplication3.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }


        [HttpPost]
        public ActionResult LongRunningDemoProcess(DemoViewModel model)
        {
            Thread.Sleep(1000);
            return Json(model, "json");
        }


    }


    public class DemoViewModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

}





<h3>Enter Your Name and Submit:</h3>

@using (Html.BeginForm("LongRunningDemoProcess", "Home", FormMethod.Post, new { id = "myform", name = "myform" }))
{
    @Html.TextBoxFor(m => m.FirstName)

    @Html.TextBoxFor(m => m.LastName)

    <input type="submit" name="operation" id="process" value="process" />
}

// We want to show this while the server process is running:
<div id="divProcessing">
    <p>Processing, please wait . . . <img src="https://cdn3.iconfinder.com/data/icons/pictofoundry-pro-vector-set/512/Alien-16.png"></p>
</div>

// We want to display the result from our submission in here:
<div id="divResult">

</div>


<script type="text/javascript">

    $(document).ready(function () {

      // Hide the "busy" Gif at load:
      $("#divProcessing").hide();

      // Attach click handler to the submit button:
      $('#process').click(function () {
          $('#myform').submit();
      });

      // Handle the form submit event, and make the Ajax request:
      $("#myform").on("submit", function (event) {
        event.preventDefault();

        // Show the "busy" Gif:
        $("#divProcessing").show();
        var url = $(this).attr("action");
        var formData = $(this).serialize();
        $.ajax({
          url: url,
          type: "POST",
          data: formData,
          dataType: "json",
          success: function (resp) {

            // Hide the "busy" gif:
            $("#divProcessing").hide();

            // Do something useful with the data:
            $("<h3>" + resp.FirstName + " " + resp.LastName + "</h3>").appendTo("#divResult");
          }
        })
      });
    });
</script>

1 个答案:

答案 0 :(得分:0)

您不应该在“提交”按钮上单独使用“单击”处理程序。我怀疑这就是它发射两次的原因:

$('#process').click(function () {
      $('#myform').submit();
  });

您明确告知要提交的表单,但在表单中按type="submit"的任何按钮都会自动触发提交事件,而无需额外的代码。因此,您有效地订购了表单的额外提交。删除点击处理程序,它应该只提交一次。