jquery 验证(使用 jquery 验证插件)- 根据单击的按钮激活特定元素的验证规则
好吧,我在这里遇到了一个恼人的问题。我在研究中了解到,多次使用 validate() 是行不通的。因此,我设置了一个函数来响应单击的某些按钮的 onclick。这些按钮发送一个参数来标识要验证哪些元素。我这样做是因为我正在使用 .fadeOut()/.fadeIn() 从一个部分无缝移动到另一个部分。一旦您单击第一部分按钮(这会将您带到第二部分),它会验证第一部分中的元素,如果第一部分被视为“有效”,那么它将继续淡出/淡入到 b 部分......在。
问题是,虽然我已经为 a 和 b 部分设置了规则和消息,但 b 部分似乎仍然没有验证。
这是我的代码 -
function validation(page) {
var rules;
var messages;
var validator;
if (page == 'secA') {
rules = {
/*gneral information validation*/
gCompanyTxt: {
required: true,
minlength: 2
}
messages = {
/*general info validator messages*/
gCompanyTxt: {
required: "The 'Company' text box is empty safgadfgad",
minlength: "The 'Company' text box needs to have at least 2 characters"
}
};
validator = new jQueryValidatorWrapper("form1", rules, messages);
} else if (page == 'secB') {
rules = {
txtFirst: {
required: true,
minlength: 2
}
};
messages = {
txtFirst: {
required: "Your first name is required",
minlength: "Your first name needs to be at least two characters"
}
};
validator = new jQueryValidatorWrapper("form1", rules, messages);
}
if (!validator.validate()) {
return;
} else {
if (page == 'secA') {
$('#secA').hide();
$('#secB').fadeIn('slow');
} else if (page == 'secTwo') {
$('#secB').hide();
$('#secC').fadeIn('slow');
}
}
}
按钮是这样的:
<button type = "button" class="buttonSale" id="btnA" onclick="validation('secA')">Proceed to Section B</button>
<button type = "button" class="buttonSale" id="btnB" onclick="validation('secB')">Proceed to Section C</button>
结果是 - 当单击“btnA”时,代码有效地工作,如果内容为空或不正确,则会触发验证,我能够修复此问题,然后继续。然而,一旦我进入 b 部分并单击“btnB”,它只会转到下一页,并且似乎跳过 if 语句:
if (!validator.validate()) {
return;
}
我想我只是在这里错过了一些非常简单的东西,但我厌倦了查看它并且需要专注于其他事情。谢谢!
Ok so, I've run into an annoying issue here. I've learned in my research that using validate() more than once does not work. So I've set up a function that responds to the onclick of certain buttons clicked. These buttons send a parameter to identify which elements to validate. I am doing this because I am using .fadeOut()/.fadeIn() to seamlessly move from section to section. Once you click the first section button (which brings you to the second section) it validates the elements in the first section, if the first section is deemed 'valid' then it proceeds to fadeOut/fadeIn to section b....and so on.
The problem is, although I have set up the rules and messages for section a and b, section b still does not seem to be validating.
Here is my code -
function validation(page) {
var rules;
var messages;
var validator;
if (page == 'secA') {
rules = {
/*gneral information validation*/
gCompanyTxt: {
required: true,
minlength: 2
}
messages = {
/*general info validator messages*/
gCompanyTxt: {
required: "The 'Company' text box is empty safgadfgad",
minlength: "The 'Company' text box needs to have at least 2 characters"
}
};
validator = new jQueryValidatorWrapper("form1", rules, messages);
} else if (page == 'secB') {
rules = {
txtFirst: {
required: true,
minlength: 2
}
};
messages = {
txtFirst: {
required: "Your first name is required",
minlength: "Your first name needs to be at least two characters"
}
};
validator = new jQueryValidatorWrapper("form1", rules, messages);
}
if (!validator.validate()) {
return;
} else {
if (page == 'secA') {
$('#secA').hide();
$('#secB').fadeIn('slow');
} else if (page == 'secTwo') {
$('#secB').hide();
$('#secC').fadeIn('slow');
}
}
}
The buttons are as such:
<button type = "button" class="buttonSale" id="btnA" onclick="validation('secA')">Proceed to Section B</button>
<button type = "button" class="buttonSale" id="btnB" onclick="validation('secB')">Proceed to Section C</button>
The result of this is - when click on 'btnA' the code works effectively, if things are empty or incorrect, the validation fires and I am able to fix this and then move on. However, once I am in section b and click the 'btnB', it merely moves on to the next page and seemingly skips the if statement:
if (!validator.validate()) {
return;
}
I think I am just missing something really simple here, but I am tired of looking at it and need to focus on other things. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用 jQuery.Validate 吗?如果是这样,它只会验证屏幕上可见的元素,因此您不必执行任何特殊逻辑来更改规则。只需立即分配所有规则并确保它忽略隐藏的输入。
完成后,您可以绑定每个按钮上的单击事件,以使用
$(yourForm).validate().form()
检查表单的验证状态,并在返回时分页到下一个面板正确(有效)。流程中的最后一个按钮应该实际提交。Is this using jQuery.Validate? If so, it will only validate the elements visible on the screen, so you shouldn't have to do any special logic to change the rules. Just assign all your rules at once and make sure it ignores hidden inputs.
Once that's done you can bind the click event on each button to check the validation state of the form using
$(yourForm).validate().form()
and page to the next panel if it comes back true (valid). The last button in your flow should actually submit.