简体   繁体   English

显示表单上所有选定单选按钮的值

[英]display the value of all selected radio buttons on a form

I have multiple radio buttons generated in a php loop which looks something like this 我在php循环中生成了多个单选按钮,看起来像这样

while(){

     <input type="radio" id="picks'.$x.'" name="picks['.$x.']" value="'.$row['team1'].' " onclick="return disp()""><span>'.$team1.'</span>

   <input type="radio" id="picks'.$x.'"  name="picks['.$x.']" value="'.$row['team2'].' "onclick="return disp()""><span>'.$team2.'</span>

    <input type="radio" name="picks'.$x.'" value="draw" onclick="return disp()">

}

在此处输入图片说明

What I want to do 我想做的事

Display all selected radio buttons in a div on the bottom of page 在页面底部的div中显示所有选中的单选按钮

My Code 我的密码

var elmnts = document.getElementById("makePicksForm").elements
var lngth = document.getElementById("makePicksForm").length;
var div = document.getElementById("dispPicks");

for (var x = 0; x < lngth; x++) {
    if (elmnts[x].type == "radio" && elmnts[x].checked == true) {
        div.innerHTML = elmnts[x].value;
    }
}

My Problem 我的问题

Only the value of first selected radio button is displayed in div, other radio buttons are ignored div中仅显示第一个选定单选按钮的值,其他单选按钮将被忽略

在此处输入图片说明

My Question 我的问题

Any idea how I can modify my javascript to display the values of ALL selected radio buttons? 知道如何修改JavaScript以显示所有选定单选按钮的值吗?

Since you've tagged your question with jQuery, here is a jQuery solution. 由于您已使用jQuery标记了问题,因此这里提供了jQuery解决方案。 Run the snippet to see it work: 运行代码片段以查看其工作原理:

 $(document).ready(function () { $(':radio').change(function (e) { //clear the div $('#dispPicks').html(''); //update the div $(':radio:checked').each(function (ind, ele) { $('#dispPicks').append($(ele).val() + '<br/>'); }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="radio" name="foo" value="foo1" /> <input type="radio" name="foo" value="foo2" /> <input type="radio" name="foo" value="foo3" /> <br/> <input type="radio" name="bar" value="bar1" /> <input type="radio" name="bar" value="bar2" /> <input type="radio" name="bar" value="bar3" /> <br/> <input type="radio" name="wow" value="wow1" /> <input type="radio" name="wow" value="wow2" /> <input type="radio" name="wow" value="wow3" /> <div id="dispPicks"></div> 

You're using lngth in your for loop, but that's defined by getting an element by ID which should only be 1 element. 您在for循环中使用了lngth ,但这是通过按ID获取元素(仅应为1个元素)来定义的。 Your loop will only run once that way... 这样循环只会运行一次...

Assuming the element with ID makePicksForm contains all your radio buttons, you need to get the length of the elements: 假设ID为makePicksForm的元素包含所有单选按钮,则需要获取元素的长度:

var elmnts = document.getElementById("makePicksForm").elements;
var div = document.getElementById("dispPicks");

for (var x = 0; x < elmnts.length; x++) {
    if (elmnts[x].type == "radio" && elmnts[x].checked == true) {
        div.innerHTML += elmnts[x].value;
    }
}

Also, you need to add the value to the innerHTML property, using += 另外,您需要使用+=将值添加到innerHTML属性中

as a side note: your PHP loop is creating duplicate ID's, which will result in failures in your javascript code if you need to reference the elements... 附带说明:您的PHP循环正在创建重复的ID,如果您需要引用这些元素,则会导致JavaScript代码失败...

Another jQuery-Fiddle 另一个jQuery小提琴

<input type="radio" id="bob" name="boys" value="Bob"><label for="bob">Bob</label><br>
<input type="radio" id="jim" name="boys" value="Jim"><label for="jim">Jim</label><br>
<input type="radio" id="pete" name="boys" value="Pete"><label for="pete">Pete</label><br>
<input type="radio" id="mary" name="girls" value="Mary"><label for="mary">Mary</label><br>
<input type="radio" id="jane" name="girls" value="Jane"><label for="jane">Jane</label><br>
<input type="radio" id="susan" name="girls" value="Susan"><label for="susan">Susan</label>

<h3><span id="boy">?</span> and <span id="girl">?</span></h3>

$("input[name=boys]").click(function () {
    $("#boy").text($(this).val());
});

$("input[name=girls]").click(function () {
    $("#girl").text($(this).val());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM