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 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名