我使用jQuery和jQuery的验证插件来验证输入。 下面是代码。 现在有一个名为像问题1,问题2,问题3,问题4多投入,......我怎样才能把验证他们? 我的意思是如何选择它们放在一起?
$(document).ready(function() {
$("#item").validate({
rules: {
title: {
required: true,
minlength:40
},
content: {
required: true,
minlength:100,
maxlength:2000
}
},
messages: {
}
});
});
编码:
$("input[name^='question']"): {
required: true,
minlength:40
}
不工作。
有几种方法。 你可以用逗号分隔符:
$("#question1, #question2, #question3")...
您可以使用add()
$("#question1").add("#question2").add("#question3")..
如果问题1是一个名字,而不是一个ID,使用属性选择:
$(":input[name^=question]")...
但我会建议使用类:
<input type="text" name="question1" class="question">
<input type="text" name="question2" class="question">
<input type="text" name="question3" class="question">
有:
$(":input.question")...
假设你的意思<input type="text" name="question1" />
然后尝试以下jQuery选择:
$("input[name^='question']");
这将返回所有这些元素的列表。
这是如何做到这一点(假设你张贴一个单元工作的代码):
$(document).ready(function() {
$("input[name^='question']").validate({
rules: {
title: {
required: true,
minlength:40
},
content: {
required: true,
minlength:100,
maxlength:2000
}
},
messages: {
}
});
});
文章来源: How to select elements like question1, question2, question3,… in JQuery form validation plug-in?