热来获取组中单选按钮的值

时间:2013-09-26 16:26:04

标签: javascript jquery radio-button

如何从2个不同的组中获取单选按钮的值(DIV:步骤1和步骤2)?实际上我有5个,但这将是同一个故事。

初始化

        var a1Id;
        var a2Id; 
        var a3Id;
        var a4Id; 
        var a5Id;
        var a6Id; 

a1Id = questionaireResult.questions[0].answers[0].id; (value of id = 1)
            document.getElementById('antwort1fuerFrage1').value = a1Id;

 a2Id = questionaireResult.questions[0].answers[1].id; (value of id = 2)
                document.getElementById('antwort2fuerFrage1').value = a2Id;

and so on for 3,4,5, and 6...

JQUERY 我希望读取所选单选按钮的值。为每个小组做这件事

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

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

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>

1 个答案:

答案 0 :(得分:1)

试试这个

var step1=$("#step-1 input:radio[name='group1']:checked").val();
 var step2= $("#step-2 input:radio[name='group2']:checked").val();
alert(step1+' '+ step2);