如果条件为真,检查验证脚本不返回

我有一个包含复选框的表单:


<form action="tienda3.php">

    <div class="form-group">

        <label for="email">Email address:</label>

        <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email to confirm the order">

    </div>



    <div class="form-group">

        <div class="checkbox">

            <label><input type="checkbox" id="TOS" value="This"> I certify that I am of legal age and I have read and agree to the 

                <a href="../terms.php" target="_blank">Terms of use</a> and

                <a href="../privacy.php" target="_blank"> Privacy Policy </a>of Sdocks LLC</label>

            </div>

        </div>

        <button type="submit" onclick="validate()" class="btn btn-primary">Submit</button>

    </form>

我需要验证用户是否选中复选框以将表单发布到 tienda3.php。


我使用此脚本来验证用户是否已选中该复选框:


<script type=text/javascript>

    function validate(){

        if (document.getElementById('TOS').checked){

            alert("checked") ;

        }else{

            alert("You didn't check it! Let me check it for you.");

            return true;

        }

    }

</script>

如果选中该复选框,则表单将发布到 tienda3.php,否则必须显示警报,通知用户必须选中该复选框才能继续该过程。


就我而言,表单始终发布到 tienda3.php。该脚本检测复选框是否被选中,但在这两种情况下,表单始终打开文件 tienda3.php


慕妹3146593
浏览 97回答 2
2回答

慕村9548890

我建议您进行以下更改:/* replace this: * <form action="tienda3.php"> */ <form action="tienda3.php" onsubmit="return validate(event)" > /* replace this:  * <button type="submit" onclick="validate()" class="btn btn-primary">Submit</button> */<button type="submit" class="btn btn-primary">Submit</button>/* and replace the validate() function with: */function validate(event){  if (document.getElementById('TOS').checked){    alert("checked") ;    return true;  } else {    event.preventDefault();    alert("You didn't check it! Let me check it for you.");    return false;  }}让我知道它是否按预期工作。

绝地无双

我更喜欢使用ajax请求而不是表单操作超文本标记语言<form>&nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp;<label for="email">Email address:</label>&nbsp; &nbsp; &nbsp; &nbsp;<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email to confirm the order">&nbsp; &nbsp;</div>&nbsp; &nbsp;<div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <div class="checkbox">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label><input type="checkbox" id="TOS" value="This"> I certify that I am of legal age and I have read and agree to the&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="../terms.php" target="_blank">Terms of use</a> and&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="../privacy.php" target="_blank"> Privacy Policy </a>of Sdocks LLC</label>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp;<button type="button" onclick="SubmitRequest()" class="btn btn-primary">Submit</button></form>jsfunction SubmitRequest() {&nbsp; if (document.getElementById('TOS').checked){&nbsp; var postObj = {&nbsp; &nbsp; &nbsp; email: $('#email').val(),&nbsp; };&nbsp; $.ajax({&nbsp; &nbsp; &nbsp; url: "/tienda3.php",&nbsp; &nbsp; &nbsp; data: JSON.stringify(postObj),&nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; contentType: "application/json;charset=utf-8",&nbsp; &nbsp; &nbsp; dataType: "json",&nbsp; &nbsp; &nbsp; success: function (result) {&nbsp; &nbsp; &nbsp; &nbsp;console.log(result)&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; error: function (errormessage) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(errormessage);&nbsp; &nbsp; &nbsp; }&nbsp; });}});
打开App,查看更多内容
随时随地看视频慕课网APP