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 readimage函数怎么读取变量图片地址
  • ¥50 网课里面实习定位打卡
  • ¥50 Delphi 非客户区窗口阴影?
  • ¥15 cv2 morphologyEx函数报错
  • ¥15 有没有知道鸿蒙OS高级开发者新题答案的
  • ¥15 有没有人能帮我一下android
  • ¥20 做一个干部信息管理系统 软件
  • ¥15 通过4G模块EC600N向阿里云物联网平台物模型上面发送字符串,现在发送int数据是成功的,发送字符串就是不成功
  • ¥15 IDA反编译,代码识别失败
  • ¥70 matlab代码修改