admin管理员组文章数量:1415420
I am a jquery newbie. I could get the validation rules working in my form. I am using a Java framework and for some reason, when it converts from .xhtml to .html, all the ui ponent's id/name were prefixed with the form that containing it. I would like to reuse these validation rules for two other forms but because of the prefix issue, I would need to use js variable and pass the formId to the function. For some reason, this gives me a problem:
NOT WORKING
var positionTitle = "#myForm:positionTitle";
$("#" + formId).validate({
rules:{
positionTitle : {required:true, maxlength:50},
...
WORKING
$("#" + formId).validate({
rules:{
"#myForm:positionTitle" : {required:true, maxlength:50},
...
I am a jquery newbie. I could get the validation rules working in my form. I am using a Java framework and for some reason, when it converts from .xhtml to .html, all the ui ponent's id/name were prefixed with the form that containing it. I would like to reuse these validation rules for two other forms but because of the prefix issue, I would need to use js variable and pass the formId to the function. For some reason, this gives me a problem:
NOT WORKING
var positionTitle = "#myForm:positionTitle";
$("#" + formId).validate({
rules:{
positionTitle : {required:true, maxlength:50},
...
WORKING
$("#" + formId).validate({
rules:{
"#myForm:positionTitle" : {required:true, maxlength:50},
...
Share
Improve this question
asked Oct 1, 2010 at 13:36
blu3_ros3blu3_ros3
331 silver badge3 bronze badges
1 Answer
Reset to default 5After you call .validate()
on the form, you an do this:
$("[name='" + positionTitle + "']").rules("add", {required:true, maxlength:50});
...but you can't use the variable as a name in an object literal like that.
The other option is constructing the rules
object before it gets passed into validate()
, but that gets a bit messy as well, I can't say which is better based on the question, but if you have many of these, you may want to go the rules
building route.
本文标签: javascriptUse field name variable in JQuery validation rulesStack Overflow
版权声明:本文标题:javascript - Use field name variable in JQuery validation rules - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745214059a2648057.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论