[英]Form validation - at least one checkbox is required in a group
I have found several examples and solutions, but none seemed to work for me the way I want to.我找到了几个例子和解决方案,但似乎没有一个像我想要的那样适合我。
What I need is this: I have a form in which I have to select what we can call "properties" for each of 23 "users", and at least one is required.我需要的是:我有一个表格,我必须在其中为 23 个“用户”中的每一个选择我们可以称之为“属性”的东西,并且至少需要一个。
It should visually be like a spreadsheet, with lines and columns:它在视觉上应该像一个电子表格,有行和列:
What I haven't been able to do is make sure at least one Option is selected for each and every UserID .我无法做的是确保为每个UserID选择至少一个Option 。
Can you point me in the right direction, for what I should use?你能指出我应该使用的正确方向吗? It can be a coding standard, or a tool... I don't know...
它可以是编码标准,也可以是工具……我不知道……
There are many ways to solve this problem.有很多方法可以解决这个问题。 I can show you one of them.
我可以给你看其中之一。
Note: I'm not that good in coding.注意:我不太擅长编码。 I don't completely know if it is a good practice or not but it works.
我不完全知道这是否是一个好习惯,但它有效。
class
will be used for validation and name
will be used to get your data after submitting. class
将用于验证, name
将用于在提交后获取您的数据。 Here is a sample code: I used 4 options and 4 user id only.这是一个示例代码:我只使用了 4 个选项和 4 个用户 ID。 you can change it accordingly.
你可以相应地改变它。
<table class="table table-bordered">
<tr>
<td>
UserID
</td>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
<td>
4
</td>
</tr>
<tr>
<td>
Option A
</td>
<td>
<input type="checkbox" class="checkbox1" name="user1option" value="a" />
</td>
<td>
<input type="checkbox" class="checkbox2" name="user2option" value="a" />
</td>
<td>
<input type="checkbox" class="checkbox3" name="user3option" value="a" />
</td>
<td>
<input type="checkbox" class="checkbox4" name="user4option" value="a" />
</td>
</tr>
<tr>
<td>
Option B
</td>
<td>
<input type="checkbox" class="checkbox1" name="user1option" value="b" />
</td>
<td>
<input type="checkbox" class="checkbox2" name="user2option" value="b" />
</td>
<td>
<input type="checkbox" class="checkbox3" name="user3option" value="b" />
</td>
<td>
<input type="checkbox" class="checkbox4" name="user4option" value="b" />
</td>
</tr>
<tr>
<td>
Option C
</td>
<td>
<input type="checkbox" class="checkbox1" name="user1option" value="c" />
</td>
<td>
<input type="checkbox" class="checkbox2" name="user2option" value="c" />
</td>
<td>
<input type="checkbox" class="checkbox3" name="user3option" value="c" />
</td>
<td>
<input type="checkbox" class="checkbox4" name="user4option" value="c" />
</td>
</tr>
</table>
Second, in your form add an onsubmit() function for validation.其次,在您的表单中添加一个 onsubmit() 函数进行验证。
<form action="submitaction" onsubmit="return validateForm()">...
*Lastly, add this script for validation *最后,添加此脚本进行验证
<script type="text/javascript">
function validateForm() {
var checked = true;
// create a loop for checking
//for loop will be based on the no of userid in this sample its 4
for (var i = 1; i <= 4; i++) {
//will check the classname if it is checked
if ($('input.checkbox' + i).is(':checked'))
checked = true;
else
checked = false;
// will break the loop if the returned check is false in the checked options
if (checked == false)
break;
};
if (checked == false)
alert("form not submitted.");
return checked;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.