[英]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> <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>
您可以在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请求
您可以像这样使用表单的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.