Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何阻止窗体自动重置?_Javascript_Jquery - Fatal编程技术网

Javascript 如何阻止窗体自动重置?

Javascript 如何阻止窗体自动重置?,javascript,jquery,Javascript,Jquery,有谁能告诉我,在返回到表单并重置之前,如何阻止表单页面仅在几分之一秒内显示此jquery脚本的结果 我打算让表单向下滚动到显示结果的锚点#results。这是可行的,但是结果只在一瞬间显示,页面会快速返回到表单a重置 我确信我应该使用returnfalse或/和e.preventDefault()

有谁能告诉我,在返回到表单并重置之前,如何阻止表单页面仅在几分之一秒内显示此jquery脚本的结果

我打算让表单向下滚动到显示结果的锚点
#results
。这是可行的,但是结果只在一瞬间显示,页面会快速返回到表单a重置

我确信我应该使用
returnfalse或/和
e.preventDefault()
$(document).ready(function() {
    $("#message").hide();
    $("#myform").validate({
        submitHandler: function() {
            $('#gauge').empty();
            var formdata = $("#myform").serialize();
            //Post form data
            $.post('php_includes/insert.php', formdata, function(data) {
                //Process post response
            });
            //Reset Form
            $('#myform')[0].reset();
            fetchRowCount();
        }
    });
    //Fetch data from server
    function fetchRowCount() {
        $.ajax({
            url: 'php_includes/server.php',
            dataType: "json",
            success: function(data) {
                $("#rows").html(data.rows);
                $("#min").html(data.min);
                $("#max").html(data.max);
                $("#mean").html(data.total);
                $("#last").html(data.last_entry);
                //Show gage once json is receved from server
                var gage = new JustGage({
                    id: "gauge",
                    value: data.total,
                    min: data.min,
                    max: data.max,
                    title: "Sample Data"
                });
            }
        });
        //Scroll to Results
        $('html, body').animate({
            scrollTop: $('#results').offset().top
        }, 'slow');
        $("#gauge").fadeIn(slow);
    }
});

也许您只需要将滚动条移动到成功承诺:

  //Fetch data from server
  function fetchRowCount() {
      $.ajax({
          url: 'php_includes/server.php',
          dataType: "json",
          success: function(data) {
              $("#rows").html(data.rows);
              $("#min").html(data.min);
              $("#max").html(data.max);
              $("#mean").html(data.total);
              $("#last").html(data.last_entry);

              //Show gage once json is receved from server

              var gage = new JustGage({
                  id: "gauge",
                  value: data.total,
                  min: data.min,
                  max: data.max,
                  title: "Sample Data"
              });

              //Scroll to Results
              $('html, body').animate({
                  scrollTop: $('#results').offset().top
              }, 'slow');
              $("#gauge").fadeIn(slow);

          }
      });
  }
  //Fetch data from server
  function fetchRowCount() {
      $.ajax({
          url: 'php_includes/server.php',
          dataType: "json",
          success: function(data) {
              $("#rows").html(data.rows);
              $("#min").html(data.min);
              $("#max").html(data.max);
              $("#mean").html(data.total);
              $("#last").html(data.last_entry);

              //Show gage once json is receved from server

              var gage = new JustGage({
                  id: "gauge",
                  value: data.total,
                  min: data.min,
                  max: data.max,
                  title: "Sample Data"
              });
              //Scroll to Results
              $('html, body').animate({
                  scrollTop: $('#results').offset().top
              }, 'slow');
              $("#gauge").fadeIn(slow);
          }
      });
  }

实际上,您正在尝试在表单数据成功提交之前从服务器获取数据

尝试将post之后应执行的内容放入其回调中:

//Post form data
$.post('php_includes/insert.php', formdata, function(data) {
//Process post response
    //Reset Form
    $('#myform')[0].reset();
    fetchRowCount();
});
向下滚动时也是如此。您应该在成功从服务器获取数据后滚动