admin管理员组文章数量:1389779
Alright i have a for loop in php and it generates a group of radio buttons for a person, each radio button within the group has the same name.
for ($i = 0; $i < $count; $i++) {
echo '<tr>';
echo '<td>' . $something[$i] . '</td>';
echo '<td align="center"><input class="one" type="radio" name="' . $i . '" value="1"/></td>';
echo '<td align="center"><input class="half" type="radio" name="' . $i . '" value="0.5"/></td>';
echo '<td align="center"><input class="zero" type="radio" name="' . $i . '" value="0" checked="checked"/></td>';
echo '<td align="center"><input class="inactive" type="radio" name="' . $i . '" value="null"/></td>';
echo '</tr>';
}
I want to be able to select/check all the radio buttons with the same class by clicking a link/button. the variable $count
will change every few days, so i wont know the how many different radio groups there will be. I am looking for this hopefully to be in javascript
Alright i have a for loop in php and it generates a group of radio buttons for a person, each radio button within the group has the same name.
for ($i = 0; $i < $count; $i++) {
echo '<tr>';
echo '<td>' . $something[$i] . '</td>';
echo '<td align="center"><input class="one" type="radio" name="' . $i . '" value="1"/></td>';
echo '<td align="center"><input class="half" type="radio" name="' . $i . '" value="0.5"/></td>';
echo '<td align="center"><input class="zero" type="radio" name="' . $i . '" value="0" checked="checked"/></td>';
echo '<td align="center"><input class="inactive" type="radio" name="' . $i . '" value="null"/></td>';
echo '</tr>';
}
I want to be able to select/check all the radio buttons with the same class by clicking a link/button. the variable $count
will change every few days, so i wont know the how many different radio groups there will be. I am looking for this hopefully to be in javascript
3 Answers
Reset to default 2You can't do this. Radio button groups allow a single selection. You will need checkboxes to acplish this.
Provided the class names are unique to the radio buttons, you could do this:
var radios = document.getElementsByTagName("INPUT");
for (var index = 0; index < radios.length; index++) {
if (radios(index).type == "radio" &&
radios(index).className.indexOf("one") > -1) {
radios(index).checked = true;
}
}
All this is probably no better than using getElementsById for each, but probably better than getElementsByClassName, which isn't universally supported, I don't think.
okay i got mine figured out here,
$(document).ready(function() {
// select all radio buttons in the "one" column (.one class)
$('#select_all_one').click(function(){
$("input[class=one]").each(function(){
this.checked = true;
});
});
// select all radio buttons in the "half" column (.half class)
$('#select_all_half').click(function(){
$("input[class=half]").each(function(){
this.checked = true;
});
});
// select all radio buttons in the "zero" column (.zero class)
$('#select_all_zero').click(function(){
$("input[class=zero]").each(function(){
this.checked = true;
});
});
// select all radio buttons in the "inactive" column (.inactive class)
$('#select_all_inactive').click(function(){
$("input[class=inactive]").each(function(){
this.checked = true;
});
});
});
and then to select all the radio i use this link <a id="select_all_one" name="select_all" value="all_one">
and change the name of the id and value to half, zero, and inactive for the respected links, everything works perfect
本文标签: javascriptSelecting multiple radio buttonsStack Overflow
版权声明:本文标题:javascript - Selecting multiple radio buttons - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744682065a2619471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论