получить выбранный переключатель из группы

0

iam пытается сделать это двумя способами, и значение обоих значений равно "". я должен получить value- "id" кнопки радиона, если он выбран.

Способ 1

 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>
Теги:
button
radio

1 ответ

2
Лучший ответ

Если вам нужен id проверенного атрибута, используйте .attr()

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

selected.val().id пытается получить свойство id value выбранного радио

Ещё вопросы

Сообщество Overcoder
Наверх
Меню