weixin_33688840 2017-02-17 09:15 采纳率: 0%
浏览 9

不开两次

Goal:
When you press on the button, the value from the textbox shall be displayed one time below the formula

Problem:
When I click on the button, the data will be displayed twice.

I tried this code below below but it doesn't work

  $('#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");
      }
  });

}

Info:
*Using ASP.net mvc and JQuery
*The foundation of this code is from this 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条回答 默认 最新

  • weixin_33721427 2017-02-17 09:39
    关注

    You shouldn't need a separate "click" handler on the "submit" button. I would suspect this is why it's firing twice:

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

    You are explicitly telling the form to be submitted, but pressing any button with type="submit" within a form will trigger the submit event automatically, without extra code. Therefore you're effectively ordering an extra submission of the form. Remove the click handler and it should just submit once.

    评论

    报告相同问题?

    悬赏问题

    • ¥15 如何利用闲置机械硬盘变现
    • ¥15 信号处理中的凸优化问题
    • ¥15 arm虚拟机无法和物理机互通
    • ¥15 如何在此代码上增加一个统计学生生源的功能?(语言-c语言)
    • ¥15 Android导航条遮盖异常
    • ¥15 计算机网络技术基础问题
    • ¥15 设置mac系统只能访问指定网站
    • ¥15 西门子博途 s7 1200控制三台步进电机
    • ¥15 基于非参数的方向距离函数求污染物影子价格(有偿)
    • ¥15 vue+element 生成table