Javascript 检查单选按钮组是否至少选择了一个
我的反馈表有问题。我想用JQuery或Javascript验证一下,在用户提交表单之前,每组单选按钮都会选择一个按钮 这是我的form.html中的代码Javascript 检查单选按钮组是否至少选择了一个,javascript,jquery,html,Javascript,Jquery,Html,我的反馈表有问题。我想用JQuery或Javascript验证一下,在用户提交表单之前,每组单选按钮都会选择一个按钮 这是我的form.html中的代码 <form id='form' method='POST' action='validate.php'> <table> <!-- Table header --> <tr> <th> </th&g
<form id='form' method='POST' action='validate.php'>
<table>
<!-- Table header -->
<tr>
<th> </th>
<th>Always</th>
<th>Often</th>
<th>Rarely</th>
<th>Never</th>
</tr>
<!-- Group One -->
<tr>
<th>Dummy Text 1</th>
<th><input class='radio' type='radio' name='item[0]' value='always'></th>
<th><input class='radio' type='radio' name='item[0]' value='often'></th>
<th><input class='radio' type='radio' name='item[0]' value='rarely'></th>
<th><input class='radio' type='radio' name='item[0]' value='never'></th>
</tr>
<!-- Group two -->
<tr>
<th>Dummy Text 2</th>
<th><input class='radio' type='radio' name='item[1]' value='always'></th>
<th><input class='radio' type='radio' name='item[1]' value='often'></th>
<th><input class='radio' type='radio' name='item[1]' value='rarely'></th>
<th><input class='radio' type='radio' name='item[1]' value='never'></th>
</tr>
<!-- End of table -->
</table>
</form>
<button class='buttons' onclick='subForm()' name='submit'>Send Feedback</button>
<script>
function subForm() {
//Code
}
</script>
总是
经常
很少地
从未
虚拟文本1
虚拟文本2
发送反馈
函数子窗体(){
//代码
}
但是我不知道我应该用什么来检查单选按钮是否被选中
我试图使用
document.getElementsByName
,但这返回了未定义的值您可以向每组单选按钮添加一个类,然后使用getelementsbyclass或queryselectorall(与旧浏览器兼容)。根据您试图支持的内容,您还可以考虑在单选按钮上使用HTML5“必需”属性。这将适用于大多数比IE8更新的浏览器,并且您只需要最少的编码
我无法发表评论,因此我将澄清,目前发布在此处的其他解决方案将不起作用,因为它会进行检查,以确保页面上至少有一个单选按钮已被选中,这意味着如果存在多组单选按钮,则用户可以提交不完整的表单。他的代码看起来很实用,否则,只需为每组单选按钮创建一个类。您可以向每组单选按钮添加一个类,然后使用getelementsbyclass或queryselectorall(与旧浏览器兼容)。根据您试图支持的内容,您还可以考虑在单选按钮上使用HTML5“必需”属性。这将适用于大多数比IE8更新的浏览器,并且您只需要最少的编码
我无法发表评论,因此我将澄清,目前发布在此处的其他解决方案将不起作用,因为它会进行检查,以确保页面上至少有一个单选按钮已被选中,这意味着如果存在多组单选按钮,则用户可以提交不完整的表单。他的代码看起来很实用,否则,只需为每组单选按钮创建一个类。我认为这是最好的选择:
var selectedCount = 0;
$('.radio').each(function(){
if($(this).attr("checked", "checked")){
selectedCount++;
}
})
我认为这是你最好的选择:
var selectedCount = 0;
$('.radio').each(function(){
if($(this).attr("checked", "checked")){
selectedCount++;
}
})
这将返回选中的单选按钮数:
$('input:radio:checked').length
检查它是否等于单选按钮的组数。(在您的示例中,有两个。)
返回选中单选按钮的数量:
$('input:radio:checked').length
检查它是否等于单选按钮的组数。(在您的示例中,有两个。)
尝试以下解决方案:
function subForm() {
var valid = true;
//for every row
jQuery("tr").each(function(idx, elem) {
//checks only rows with radio inputs inside
if ($(this).find('input[type=radio]').length) {
//if there are no radios checked then form is not valid
if (!$(this).find('input[type=radio]:checked').length) {
valid = false;
}
}
});
console.log(valid);
if (valid) {
//submit form
}
}
变量“valid”表示整个表单有效(每组至少选择一个单选按钮)
这里有一个解决方案。试试这个解决方案:
function subForm() {
var valid = true;
//for every row
jQuery("tr").each(function(idx, elem) {
//checks only rows with radio inputs inside
if ($(this).find('input[type=radio]').length) {
//if there are no radios checked then form is not valid
if (!$(this).find('input[type=radio]:checked').length) {
valid = false;
}
}
});
console.log(valid);
if (valid) {
//submit form
}
}
变量“valid”表示整个表单有效(每组至少选择一个单选按钮)
这里有一个。是的,我是指geElementsByClassName,我很懒,我通常依靠我的开发环境/代码编辑器来自动完成类似的事情。是的,我是指geElementsByClassName,我很懒,我通常依靠我的开发环境/代码编辑器来自动完成类似的事情。