只需点击两次即可提交

submit done only after two click

本文关键字:两次 提交      更新时间:2023-09-26

我需要在提交前检查值
所以我尝试了两种方法,但问题相同
问题是用户需要在检查为true后再次单击
我尝试过chrome、firefox和IE,但总是出现同样的问题
此处为index.php

<!DOCTYPE html>
<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
        submitnow=false;
        $(document).ready(function(){
            $("form#test").submit(function(e){
                //e.preventDefault(); //i test this way (here and line 17) with same problem line
                if(submitnow==false){
                        num=$("#num").val();
                        $.post("check.php",{num:num},function(result){
                            if(result=="true"){
                                alert("check return true");
                                submitnow=true;
                                $("form#test").submit();
                                //$("form#test").unbind('submit').submit();
                            }else{
                                alert("check return false");
                            }
                        });
                }
                if(!submitnow)
                    return false;
                else
                    alert("submitnow is true");
            });
        });
    </script>
<body>
    <form action="" method="POST" id="test" >
        <label for="num" >Num</label>
        <input id="num" name="num" type="text"  value="1" required/>
        </br>
        <input id="submit" type="submit" name="submit"/>
    </form>
<?php
echo (isset($_POST['submit']))?'submit done':'';
?>
</body>
</html>

在这里检查.php

<?php
echo ($_POST['num']==1)?'true':'false';
?>

尝试

$(document).ready(function () {
    $("form#test").submit(function (e) {
        e.preventDefault();
        var frm = this;
        var num = $("#num").val();
        $.post("check.php", {
            num: num
        }, function (result) {
            if (result == "true") {
                alert("check return true");
                submitnow = true;
                //don't do this as it will call the submit handler again
                //$("form#test").submit();
                frm.submit();
            } else {
                alert("check return false");
            }
        });
    }
    });
});

然后更改submit按钮的id和名称

<input id="mysubmit" type="submit" name="mysubmit"/>
   $("form#test").submit(function(e){
            //e.preventDefault(); //i test this way (here and line 17) with same problem line
            if(submitnow==false){
                    num=$("#num").val();
                    var formname = $this;
                    $.post("check.php",{num:num},function(result){
                        if(result=="true"){
                            alert("check return true");
                            submitnow=true;
                            formname.submit();
                            //$("form#test").unbind('submit').submit();
                        }else{
                            alert("check return false");
                        }
                    });

这对你有用。当你提交内置表格时,有两种类型。