簡體   English   中英

如何停止表單提交並拋出錯誤jQuery

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

嗨,我在彈出的按鈕中提交了支付網關信息。 我想要的就是當未選中付款方式(單選按鈕)或接受條款(復選框)時我必須拋出錯誤,如果選擇了任何一個則拋出錯誤以檢查另一個,並且如果都選中則打開新的小型瀏覽器。 我曾使用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>

您可以在form上放置onsubmit函數調用,並在驗證失敗時返回false。

HTML:

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

javascript:

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

單擊submitBtn ,可以通過preventDefault函數將其停止,例如:

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

嘗試使用Jquery驗證,並在SubmitHandler內可以調用ajax請求

jQuery驗證示例

您可以像這樣使用表單的submit事件進行表單驗證

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM