嗨我在弹出窗口中有提交支付网关信息的按钮。我想要的只是当支付方法(单选按钮)或接受条款(复选框)没有被检查时我必须抛出错误,如果选择任何一个然后抛出错误检查另一个并且如果两个都被检查然后打开新的迷你浏览器。我使用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> <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> <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> <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>
答案 0 :(得分:2)
您可以将onsubmit
函数调用放在form
上并返回false以进行验证失败。
HTML:
<form onsubmit="validate()"....>
的javascript:
function validate()
{
//your logic to validate data
if(valid data)
return true;
else
return false;
}
答案 1 :(得分:2)
点击submitBtn
后,您可以按功能preventDefault
停止,例如:
$("#submitBtn").click(function(event){
event.preventDefault();
/// check validate conditions
if(validate)
$("#formID").submit();
else
throw errors;
});
答案 2 :(得分:0)
尝试使用Jquery验证,在submitHandler中,您可以调用ajax请求
答案 3 :(得分:0)
您可以使用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>