gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:01:40 25 4
gpt4 key购买 nike

您好,我在弹出窗口中有一个提交支付网关信息的按钮。我想要的只是当付款方式(单选按钮)或接受条款(复选框)未选中时我必须抛出错误,如果选择了任何一个,则抛出错误以检查另一个,如果两者都被选中,则打开新的迷你浏览器。我使用 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>

最佳答案

当点击submitBtn时,可以通过函数preventDefault停止,例如:

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

关于javascript - 如何停止表单提交并抛出错误jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31308923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com