jQuery/js提交表单点击


jQuery/js submit form on click

禁用提交按钮后无法提交表单。我做了一个jquery代码,以防止多次提交它的工作,但表单不发送POST请求。

jQuery:

$(document).ready(function() { 
var enblereg = function(e) {
    $(e).removeAttr("disabled");
}
$("#goRegister").click(function() {
$(this.form).submit();
    var reg = this;
    $('#reg').submit();
    $(this).attr("disabled", true);
    setTimeout(function() { enblereg(reg) }, 2000);
});
});
php:

<?php if (isset($_POST['goRegister'])) { echo "send";} ?>
    <form action="?" method="POST" id="reg">
        <h6>Full Name: <?php echo $fnamerr; ?></h6>
        <input type="text" name="fname" value="<?php echo $fullname;?>">
        <h6>Email: <?php echo $emailerr; ?></h6>
        <input type="text" name="email" value="<?php echo $email;?>">
        <h6>Re-enter Email: <?php echo $aemailerr; ?></h6>
        <input type="text" name="aemail" value="<?php echo $aemail;?>">
        <h6>password: <?php echo $passworderr; ?></h6>      
        <input type="password" name="password">
        <h6>Re-enter password: <?php echo $apassworderr; ?></h6>        
        <input type="password" name="apassword">
        <h6>Birthday: <?php echo $bdayerr;?></h6>   
        <?php include ("/incs/birthdayinc.php");?>
        <h6>Gender: <?php echo $gendererr; ?></h6>
        <input type="radio" name="gender" value="female"><font size="1"><b>Female</b></font>
        <input type="radio" name="gender" value="male"><font size="1"><b>Male</b></font><br>
        <input type="submit" name="goRegister" id="goRegister" value="Register">
      </form>

没有jQuery的后期工作,我试图解决它与"ONCLICK":

this.form.submit() -不起作用

在禁用提交按钮之前,我需要在jQuery中添加什么来提交表单

这个过程的jQuery代码应该是这样的

$(this.form).submit();

这将为您提交表单。如果它不会触发事件。然后,您可以捕获表单并使用它的id直接提交它。

id="reg"

所以应该是

$('#reg').submit();

这样做…表单的id为#reg。所以只需使用$('#reg').submit();

$("#goRegister").click(function() {
  var reg = this;
  $('#reg').submit();
  $(this).attr("disabled", true);
  setTimeout(function() { enblereg(reg) }, 2000);
 });

编辑

更新ajax提交

 $("#goRegister").click(function() {
  var reg = $(this);
  var form = $('#reg').serialize();
  var url = "Whereveryouareposting.php";
  //Submit ajax form
  $.post(url,form,function(data){
     //This is where you could send back error messages or success from the server
     // To dictate wether to hide the button, or display errors.
     // Ie On success....do this...
     $(this).attr("disabled", true);
     setTimeout(function() { enblereg(reg) }, 2000);
     // On error, send a message packet with the errors, and loop through it to attach error 
     //messages to fields
  });
 });