javascript - 从组中获取选定的单选按钮

标签 javascript jquery radio-button

我尝试用两种方法来做到这一点,并且两次的值都是“”。如果选择了单选按钮,我需要获取它的值“id”。

方法一

 var selected = $("#step-1 input[type='radio']:checked");
                if (selected.length > 0) {
                    var answerIDQ1 = selected.val().id;
                }

                var selected = $("#step-2 input[type='radio']:checked");
                if (selected.length > 0) {
                    var answerIDQ2 = selected.val().id;

方式 2 }

                if ($("input[type='radio'].group1").is(':checked')) {
                    var answerIDQ1 = $("input[type='radio'].group1:checked").val().id;

                }

                if ($("input[type='radio'].group2").is(':checked')) {
                    var answerIDQ2 = $("input[type='radio'].group2:checked").val().id;

                }

HTML

<div id="step-1">
                <h2 class="StepTitle">
                    <label style="font-weight: bold; color: #662819;" id="frage1"></label>
                </h2>
                <br />

                <input type="radio" class="group1" name="group1" id="frage1antwort1" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
                <label id="antwort1fuerFrage1"></label>
                <br />
                <br />
                <input type="radio"  class="group1" name="group1" id="frage1antwort2" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
                <label id="antwort2fuerFrage1"></label>
                <br />
                <br />
                <input type="radio"  class="group1" name="group1" id="frage1antwort3" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
                <label id="antwort3fuerFrage1"></label>
            </div>

            <div id="step-2">
                <h2 class="StepTitle">
                    <label style="font-weight: bold; color: #662819;" id="frage2"></label>
                </h2>
                <br />

                <input type="radio"  class="group2" name="group2" id="frage2antwort1" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
                <label id="antwort1fuerFrage2"></label>
                <br />
                <br />
                <input type="radio"  class="group2" name="group2" id="frage2antwort2" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
                <label id="antwort2fuerFrage2"></label>
                <br />
                <br />
                <input type="radio"  class="group2" name="group2" id="frage2antwort3" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
                <label id="antwort3fuerFrage2"></label>
            </div>

最佳答案

如果您需要选中属性的id,请使用.attr()

var answerIDQ1 = selected.attr('id'); // or selected[0].id

selected.val().id 尝试获取所选单选的 valueid 属性

关于javascript - 从组中获取选定的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19033214/

相关文章:

javascript - 如何获取要检查的自定义单选按钮?

javascript - 这个 ES6 箭头 var matchLang = (route) => new RegExp ('^' + route.replace ('*' , '.*' )).test(browLoc) 的替代品是什么?

javascript - 按内联 "style"属性对 Div 进行排序

javascript - Jquery UI 可拖动 : Align helper to mouse position

asp.net - 如何在 Silverlight 3 对象上调用 javascript 函数?

java - 将表单单选值发送到 Java servlet,返回 null

javascript - 如何捕获元标记

javascript - 将页面的所有媒体查询输出到一个列表中

javascript - 需要获取所有 applicationScope 变量名称的数组

javascript - 如果取消选中单选按钮,则删除单元格的值