我使用Jquery表单验证来验证表单输入数据。有一个
确认
检查
提交 这种形式。
代码是:
<script type="text/javascript">
function Confirmation(){
var answer = confirm("Do you really want to withdraw this amount of money from your account?")
if (answer){
return true;
}
else{
return false;
}
}
$(document).ready(function() {
$("#withdraw").validate({
rules: {
amount: {
required: true,
digits:true
} ,
bank:{
required:true,
},
cardnumber1: {
required: true,
minlength:8
},
cardnumber2:{
required:true,
equalTo: "#cardnumber1"
},
holder:{
required:true,
}
}
})
});
</script>
我希望Jquery表单验证在Confirmation()
之前执行,怎么做?
答案 0 :(得分:0)
我查看了documentation并且有一个名为submitHandler的选项,我认为可以在提交表单之前但在验证发生之后添加确认对话框。试试这个......
<script type="text/javascript">
$(document).ready(function() {
$("#withdraw").validate({
submitHandler : function(form) {
if (confirm("Do you really want to withdraw this amount of money from your account?")) {
form.submit();
}
},
rules: {
amount: {
required: true,
digits:true
},
bank:{
required:true,
},
cardnumber1: {
required: true,
minlength:8
},
cardnumber2:{
required:true,
equalTo: "#cardnumber1"
},
holder:{
required:true,
}
}
})
});
</script>
答案 1 :(得分:0)
史蒂芬,
您需要从表单中删除onsubmit
处理程序,并将其添加到validate插件的submitHandler
...
$("#withdraw").validate({
rules: {
amount: {
required: true,
digits:true
}
// .. other rules here
},
submitHandler: function(form){
if( Confirmation() )
form.submit();
}
})