我需要在提交之前检查值
所以我尝试了两种方法来做到这一点,但同样的问题
问题是用户需要在检查为真后再次单击
我尝试了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>


这里是check.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"/>

09-17 18:05
查看更多