gpt4 book ai didi

javascript - 帮助 jQuery 验证

转载 作者:行者123 更新时间:2023-11-28 02:49:28 25 4
gpt4 key购买 nike

我在表单验证编码时遇到问题。我将在下面发布代码。基本上,如果 cboAction 在另一个选择上,我希望提交表单。

cboAction 具有三个值:[Select]InsertSearch。因此,如果在组合框中选择“插入”,则表单应使用 jquery 进行验证,否则如果选择“搜索”,则应提交表单。我尝试在其中添加 if 语句,但没有成功。 :(

顺便说一句,表单验证有效,只是我无法使上述工作正常进行。希望收到某人的来信。

谢谢

jQuery.validator.addMethod(
"selectOption",
function(value, element) {
if(element.value == "nothing") {
return false;
} else {
return true;
}
},
"Please select an option."
);

$(document).ready(function() {
$("#authorForm").validate({
rules: {
cboAction: {selectOption:false}
txtName: {required:true},
txtStreet: {required:true}
txtTown: {required:true},
cboState: {selectOption:true},
txtPostcode: {required:true},
txtMobile: {required:true},
txtEmail: {required:true, email:true}
},
messages: {
cboAction: "Please select an action"
txtName: {required: "Author's Name is a required field"},
txtStreet: {required: "Street is a required field"}
txtTown: "Town is a required field",
cboState: "Please select a state",
txtPostcode: "Postcode is a required field",
txtMobile:
{
required: "Mobile is a required field",
email: "Please enter a valid email adress"
}
}
});
});

最佳答案

您必须手动触发验证:

http://docs.jquery.com/Plugins/Validation/Validator/form

首先阻止提交验证:

$("form").validate({
onsubmit: false
});

添加提交处理程序以测试 cboAction。

$('form').submit(function(event){
if ($('#cboAction').val() == 'insert' && !$('form').validate().form()){
return false;
}
return true;
});

关于javascript - 帮助 jQuery 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4096474/

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