表格提交延迟

时间:2018-03-19 06:00:56

标签: php jquery ajax

我试图在10秒后用表格值提交表格。我无法在提交函数中包含setTimeout。



setTimeout(function() {
  $('#FrmID').submit();
}, 10000);
$(document).ready(function() {
  $("#submit").click(function() {
    var grp_id = $("#grp_id").val();
    var datastr = 'grp_id=' + grp_id;

    $.ajax({
      type: 'POST',
      url: 'start_calculate.php',
      data: datastr,
      success: function() {
        //$("#msg").html("Student Successfully Added");
        //$("#msg").html("response");
      }

    });


  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="FrmID" id="FrmID">
  <input type="hidden" value="<?php echo $grp_id; ?>" name="grp_id" />
  <button type="submit" name="done" class="btn btn-sm btn-success" id="submit">Done !</button>
</form>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您的代码存在的问题是您没有将提交处理程序绑定到表单。相反,您将其绑定到提交按钮的click事件。

您需要将处理程序代码绑定到表单的submit事件:

setTimeout(function() {
  $('#FrmID').submit();
}, 10000);

$(document).ready(function() {
  $("#FrmID").submit(function() {
    var grp_id = $("#grp_id").val();
    var datastr = 'grp_id=' + grp_id;

    $.ajax({
      type: 'POST',
      url: 'start_calculate.php',
      data: datastr,
      success: function() {
        //$("#msg").html("Student Successfully Added");
        //$("#msg").html("response");
      }

    });

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="FrmID" id="FrmID">
  <input type="hidden" value="<?php echo $grp_id; ?>" name="grp_id" />
  <button type="submit" name="done" class="btn btn-sm btn-success" id="submit">Done !</button>
</form>

答案 1 :(得分:1)

您可以创建一个提交功能,然后点击success后调用它:

$(document).ready(function() {

  function submitForm() {
    setTimeout(function() {
      $('#FrmID').submit();
    }, 10000);
  }

  $("#submit").click(function() {
    var grp_id = $("#grp_id").val();
    var datastr = 'grp_id=' + grp_id;

    $.ajax({
      type: 'POST',
      url: 'start_calculate.php',
      data: datastr,
      success: function() {
        //$("#msg").html("Student Successfully Added");
        //$("#msg").html("response");
        submitForm();
      }

    });


  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="FrmID" id="FrmID">
  <input type="hidden" value="<?php echo $grp_id; ?>" name="grp_id" />
  <button type="submit" name="done" class="btn btn-sm btn-success" id="submit">Done !</button>
</form>

在行动中:

$(document).ready(function() {

  function submitForm() {
    i = 1;
    setInterval(function() {
      console.log(i++);
    }, 1000);

    setTimeout(function() {
      $('#FrmID').submit();
      alert("Student Successfully Added");
    }, 10000);
  }

  $("#submit").click(function(e) {
    e.preventDefault();
    submitForm();

  });


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="FrmID" id="FrmID">
  <input type="hidden" value="<?php echo $grp_id; ?>" name="grp_id" />
  <button type="submit" name="done" class="btn btn-sm btn-success" id="submit">Done !</button>
</form>

如果您想自动提交表单而无需按任何键提交,您可以试试这个。您应该在setTimeout函数中移动代码:

$(document).ready(function() {

  setTimeout(function() {

    $('#FrmID').submit(function() {
      var grp_id = $("#grp_id").val();
      var datastr = 'grp_id=' + grp_id;

      $.ajax({
        type: 'POST',
        url: 'start_calculate.php',
        data: datastr,
        success: function() {
          //$("#msg").html("Student Successfully Added");
          //$("#msg").html("response");
        }

      });
    });

  }, 10000);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="FrmID" id="FrmID">
  <input type="hidden" value="<?php echo $grp_id; ?>" name="grp_id" />
  <button type="submit" name="done" class="btn btn-sm btn-success" id="submit">Done !</button>
</form>

或者您可以尝试delay()

$(document).ready(function() {

    $('#FrmID').delay(10000).submit(function() {
      var grp_id = $("#grp_id").val();
      var datastr = 'grp_id=' + grp_id;

      $.ajax({
        type: 'POST',
        url: 'start_calculate.php',
        data: datastr,
        success: function() {
          //$("#msg").html("Student Successfully Added");
          //$("#msg").html("response");
        }

      });
    });

});

答案 2 :(得分:0)

您可以使用以下代码。

$(document).ready(function() {

  $("#submit").click(function() {

   setTimeout(function() {
    var grp_id = $("#grp_id").val();
    var datastr = 'grp_id=' + grp_id;

    $.ajax({
      type: 'POST',
      url: 'start_calculate.php',
      data: datastr,
      success: function() {
        //$("#msg").html("Student Successfully Added");
        //$("#msg").html("response");
      }

    });

   }, 10000);
  });

});

点击提交后,只需在延迟后执行您的代码。