Javascript 如何停止表单提交并抛出错误jquery

Javascript 如何停止表单提交并抛出错误jquery,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,嗨,我在弹出窗口中有一个提交支付网关信息的按钮。我想要的是,当“付款方式”单选按钮或“接受条款”复选框未选中时,我必须抛出错误,如果选择了任何一个,则抛出错误以选中另一个,如果两者都选中,则打开新的迷你浏览器。在这里,我使用AJAX提交了这些值 $(document).ready(function() { $(document).on("mouseover", ".imgpayment", function() { $(this).parent().addClass(

嗨,我在弹出窗口中有一个提交支付网关信息的按钮。我想要的是,当“付款方式”单选按钮或“接受条款”复选框未选中时,我必须抛出错误,如果选择了任何一个,则抛出错误以选中另一个,如果两者都选中,则打开新的迷你浏览器。在这里,我使用AJAX提交了这些值

$(document).ready(function() {


    $(document).on("mouseover", ".imgpayment", function() {
        $(this).parent().addClass("paymenthover");
    });

    $(document).on("mouseout", ".imgpayment", function() {
        $(this).parent().removeClass("paymenthover");
    });

    $(document).on("click", ".the-terms", function() {
        if ($(this).is(":checked")) {
            $("table#paymenttable td").each(function() {
                if ($(this).hasClass("paymentclick")) {
                    $(this).removeClass("paymentclick");
                }
            });
            if ($("#policyAgree").prop('checked') == true) {
                $("#submitBtn").removeAttr("disabled");
            } else {
                $("#paymentError").html('<span class="disablebtnInfo" id="msg" style="display:block;margin-left: 40%;"><span class="error_sign">!</span>&nbsp;<span id="errmsg">Agree terms and condition.</span></span></strong>');

            }
            $(this).parent().addClass("paymentclick");
        } else {
            $("#paymentError").html('<span class="disablebtnInfo" id="msg" style="display:block;margin-left: 40%;"><span class="error_sign">!</span>&nbsp;<span id="errmsg">Agree terms and condition.</span></span></strong>');
            $("#submitBtn").attr("disabled", "disabled");
        }
    });


    $(document).on("click", ".imgpayment", function() {
        $("table#paymenttable td").each(function() {
            if ($(this).hasClass("paymentclick")) {
                $(this).removeClass("paymentclick");
            }
        });
        if ($("#policyAgree").prop('checked') == true) {

        } else {
            $("#paymentError").html('<span class="disablebtnInfo" id="msg" style="display:block;margin-left: 40%;"><span class="error_sign">!</span>&nbsp;<span id="errmsg">Agree terms and condition.</span></span></strong>');

        }
        $(this).parent().toggleClass("paymentclick");
        $(this).parent().find(".the-terms").prop('checked', true);
        checkterms();
    });


});


$(document).on("click", "#policyAgree", function() {
    if ($(this).prop('checked') == true) {
        checkterms();
    } else {
        $("#submitBtn").attr("disabled", 'disabled');
    }

});

function checkterms() {
    if ($(".the-terms").is(":checked") && $("#policyAgree").is(":checked")) {
        $("#submitBtn").removeAttr("disabled");
    } else {
        $("#submitBtn").attr("disabled");
    }
}
和html:

<tr id="paymentmethod">
    <td>
        <input type="radio" name="payment" value="paypal" class="the-terms" required>
        <img src='<?php echo base_url() . ' contents/images/paypal.png '; ?>' class="imgpayment" style="height: 40px;">
    </td>
</tr>
<tr>
    <td>
        <input type="radio" name="payment" value="esewa" class="the-terms" required>
        <img src='<?php echo base_url() . ' contents/images/esewa.png '; ?>' class="imgpayment" style="height: 40px;">
    </td>
</tr>
<?php } ?>
<tr>
    <td style="height: 50px;">
        <input type="radio" name="payment" value="counterPay" class="the-terms" required>
        <span class="imgpayment" style="font-weight: bold"> Cash On Arrival </span>
    </td>
</tr>
<div id="action">
    <input id="type" type="hidden" name="mini" value="mini" />
    <input id="paykey" type="hidden" name="paykey" value="10" />
    <input type="button" id="popupBtn" value="Back" onclick="backbutton()" class="backBtnPayment" />
    <span id="paymentError" style="color:#0d0daf;font-size: 12px;float: left;"><?php if(!empty($msgs)){ echo $msgs; } ?></span>
    <input type="submit" id="submitBtn" value="Next" class="payment" disabled style="float: right;">
</div>
您可以对表单执行onsubmit函数调用,如果验证失败,则返回false

HTML:

您可以对表单执行onsubmit函数调用,如果验证失败,则返回false

HTML:

单击submitBtn时,默认情况下可以停止它,例如:

单击submitBtn时,默认情况下可以停止它,例如:


尝试使用Jquery验证,并在submitHandler内部调用ajax请求


尝试使用Jquery验证,并在submitHandler内部调用ajax请求

您可以使用表单的提交事件进行表单验证,如下所示

$( "#form1" ).submit(function(event){
    if(FormNotvalid){
        alert("Error");
        event.preventDefault();
    }
});
请检查此简单示例以供参考

<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $( "#form1" ).submit(function(event){
                if($("#email").val() == ""){
                    alert("Error");
                    event.preventDefault();
                }
            });
        })
    </script>
    </head>
    <body>
        <form id="form1" action="#">
            <pre>
                <input type="text" id="email" name="email"/>
                <input type="submit"/>
            </pre>
        </form>
    </body>
</html>
您可以使用表单的提交事件进行表单验证,如下所示

$( "#form1" ).submit(function(event){
    if(FormNotvalid){
        alert("Error");
        event.preventDefault();
    }
});
请检查此简单示例以供参考

<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $( "#form1" ).submit(function(event){
                if($("#email").val() == ""){
                    alert("Error");
                    event.preventDefault();
                }
            });
        })
    </script>
    </head>
    <body>
        <form id="form1" action="#">
            <pre>
                <input type="text" id="email" name="email"/>
                <input type="submit"/>
            </pre>
        </form>
    </body>
</html>

你不能减少针对特定问题的代码吗?你不能减少针对特定问题的代码吗?我尝试过preventDefault,但对我无效。我尝试过preventDefault,但对我无效
<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $( "#form1" ).submit(function(event){
                if($("#email").val() == ""){
                    alert("Error");
                    event.preventDefault();
                }
            });
        })
    </script>
    </head>
    <body>
        <form id="form1" action="#">
            <pre>
                <input type="text" id="email" name="email"/>
                <input type="submit"/>
            </pre>
        </form>
    </body>
</html>