需要验证多个选择字段的唯一、特定值
我有一个带有一组下拉字段的表单,每个字段都有默认值“0”和可用值1-3。这些作为优先选择,用户可以选择最重要的 3 个项目。每个选择字段名称都是唯一的,并填充在 PHP foreach 循环中——我在下面的示例中将其称为“N”。
<select class="variable_priority unique required" name="select-N">
<option value="0"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
“0”值用作非优先选择的默认/预设值。使用此处描述的技术,我成功修改了 JQuery Validate 以确保用户无法通过比较“variable_priority”类在多个下拉列表中选择 1、2 或 3:
$('.variable_priority').each(function () {
if ($(this).val() === value && $(this).val() > 0) {
timeRepeated++;
}
});
但是,我仍然需要验证用户确实输入了值 1、2 和3 在任何选择字段中,并且没有跳过任何字段。谁能给我一些关于如何在 JQuery Validate 上下文中执行此操作的指导?
I have a form with set of dropdown fields, each of which has a default value of "0" and available values of 1-3. These are serving as a priority selection, where the user picks their top 3 items. Each select field name is unique and populated in a PHP foreach loop -- I'm just calling it "N" in the example below.
<select class="variable_priority unique required" name="select-N">
<option value="0"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
The "0" value is used as the default/preset value for non-prioritized selections. Using the techniques described here, I have successfully modified JQuery Validate to ensure that the user cannot pick 1, 2 or 3 in more than one dropdown by comparing the class "variable_priority":
$('.variable_priority').each(function () {
if ($(this).val() === value && $(this).val() > 0) {
timeRepeated++;
}
});
However, I still need to validate that the user has indeed entered a value of 1, 2 and 3 in any of the select fields, and has not skipped any. Can anyone give me some guidance on how to do this within the context of JQuery Validate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
重新处理 JS Fiddle 的要求后。 http://jsfiddle.net/halfcube/aLvc6/
我认为这可能更接近您的要求
HTML
CSS
JavaScript
现在我必须回去工作了。
享受
After re-working the requirement on JS Fiddle. http://jsfiddle.net/halfcube/aLvc6/
I think this maybe more closer to your requirement
HTML
CSS
JavaScript
Now I must get back to work.
enjoy
我首先会让浏览器完成大部分工作,因此我会使用 HTML5 属性,例如
required
disabled
和selected
来进行良好的测量。你可以尝试这样的事情。
这应该足以允许浏览器将
blank
显示为默认值,然后一旦用户选择一个选项,他们就无法再次选择blank
。为了安全起见,我会将 JavaScript 端验证绑定到表单提交事件以确保表单有效。
像这样的东西;
此 if 语句将检查
所选
下拉列表的计数与页面上下拉列表的总数。如果它们匹配,则该表格有效。我希望这对你有帮助。
I would firstly get the browser to do most of the work, so I would use HTML5 attributes like
required
disabled
andselected
for good mesure.You could try something like this.
This should be enough to allow the browser to show
blank
as the default value then once the user selects an option they can't selectblank
again.For safe mesure I would put a JavaScript side validation binded to the form submit event to ensure the form is valid.
Something like this;
This if statement will check the count of
selected
dropdowns compared to the total number of dropdowns on the page. If they match then the form is valid.I hope this helps you out.
试试这个:
});
Try this:
});