只需点击两次即可完成提交

时间:2014-10-14 10:06:16

标签: javascript jquery

我需要在提交前检查值 所以我尝试了两种方法,但同样的问题 问题是用户需要在检查结果后再次点击
我尝试使用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';
?>

2 个答案:

答案 0 :(得分:1)

尝试

$(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"/>

答案 1 :(得分:0)

   $("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");
                        }
                    });

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