简体   繁体   English

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

[英]how to stop form submit and throw error jquery

Hi I have button in popup that submits the payment gateway information. 嗨,我在弹出的按钮中提交了支付网关信息。 All I want is when payment method (radio button) or accept terms (check box) are not checked i have to throw error, if any one is choosed then throw error to check another and if both are checked then open new mini browser. 我想要的就是当未选中付款方式(单选按钮)或接受条款(复选框)时我必须抛出错误,如果选择了任何一个则抛出错误以检查另一个,并且如果都选中则打开新的小型浏览器。 where I have used AJAX to submit these values . 我曾使用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");
    }
}

and html: 和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>

you can put onsubmit function call on form and return false for validation failed. 您可以在form上放置onsubmit函数调用,并在验证失败时返回false。

HTML: HTML:

<form onsubmit="validate()"....>

javascript: javascript:

 function validate()
{
  //your logic to validate data
  if(valid data)
    return true;
  else
   return false;
}

When click submitBtn , you can stop it by function preventDefault such as: 单击submitBtn ,可以通过preventDefault函数将其停止,例如:

$("#submitBtn").click(function(event){
      event.preventDefault();
      /// check validate conditions
      if(validate)
        $("#formID").submit();
      else
         throw errors;
});

Try to use Jquery validation and inside submitHandler you can call ajax request 尝试使用Jquery验证,并在SubmitHandler内可以调用ajax请求

Jquery validation example jQuery验证示例

You may use submit event of form for form validation like this 您可以像这样使用表单的submit事件进行表单验证

$( "#form1" ).submit(function(event){
    if(FormNotvalid){
        alert("Error");
        event.preventDefault();
    }
});

checkout this simple example for your reference 查看这个简单的示例,以供参考

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM