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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 GEO下载数据的处理报错 :函数‘Meta’标签‘"data.frame"’找不到继承方法,如何解决?
      • ¥15 DLNM模型是否可以用二分类变量作为y变量
      • ¥15 android object box 一个实体多个表怎么写
      • ¥15 temux 启用docker 服务失败
      • ¥15 Flask 使用celery发送邮件出现‘目标计算机积极拒绝‘
      • ¥60 老人用的sd卡在手机里面不知道操作了什么,导致图片和视频变成了文件,取下sd卡连接电脑就是图中的样子,后缀改为.jpg才可以,需要用系统的画图软件才能打开,文件属性还是文件,有没有批量操作的解决办法
      • ¥15 超时跳出方法代码的返回值问题
      • ¥15 汇编语言程序设计设计,ascii码求数,再求数的BCD码
      • ¥30 Mask rcnn训练自己的数据集出现问题!
      • ¥20 研究人工智能时的几个问题