最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - how to read if radio button checked and save it as 1 with javascript? - Stack Overflow

matteradmin6PV0评论

I have 3 radio buttons:

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

    <input type="radio" name="group1" id="antwort1" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
    <label id="antwort1fuerFrage1"></label>
    <br />
    <br />

    <input type="radio" name="group1" id="antwort2" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
    <label id="antwort2fuerFrage1"></label>
    <br />
    <br />

    <input type="radio" name="group1" id="antwort3" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
    <label id="antwort3fuerFrage1"></label>
</div>

I am trying to read if radio button is checked, and save a value (need 1 or/ 0) like this:

var frage1 = document.getElementById("frage1").innerHTML;
var antwort1 = document.getElementById("antwort1");
var antwort2 = document.getElementById("antwort2");
var antwort3 = document.getElementById("antwort3");

antwort1 = $('input[id="antwort1"]:checked').val();
antwort2 = $('input[id="antwort2"]:checked').val();
antwort3 = $('input[id="antwort3"]:checked').val();

antwort 1, 2, 3 are undefined. I need to get 0 or 1.

I have 3 radio buttons:

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

    <input type="radio" name="group1" id="antwort1" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
    <label id="antwort1fuerFrage1"></label>
    <br />
    <br />

    <input type="radio" name="group1" id="antwort2" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
    <label id="antwort2fuerFrage1"></label>
    <br />
    <br />

    <input type="radio" name="group1" id="antwort3" value="" onmousedown="this.__chk = this.checked" onclick="if (this.__chk) this.checked = false" />
    <label id="antwort3fuerFrage1"></label>
</div>

I am trying to read if radio button is checked, and save a value (need 1 or/ 0) like this:

var frage1 = document.getElementById("frage1").innerHTML;
var antwort1 = document.getElementById("antwort1");
var antwort2 = document.getElementById("antwort2");
var antwort3 = document.getElementById("antwort3");

antwort1 = $('input[id="antwort1"]:checked').val();
antwort2 = $('input[id="antwort2"]:checked').val();
antwort3 = $('input[id="antwort3"]:checked').val();

antwort 1, 2, 3 are undefined. I need to get 0 or 1.

Share Improve this question edited Sep 26, 2013 at 11:28 lexeme 2,97314 gold badges63 silver badges129 bronze badges asked Sep 26, 2013 at 11:05 r.rr.r 7,18329 gold badges90 silver badges132 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You could just use .length since you're querying IDs:

antwort1 = $('#antwort1:checked').length;
antwort2 = $('#antwort2:checked').length;
antwort3 = $('#antwort3:checked').length;

Use prop() like

antwort1 = $('input[id="antwort1"]').prop();
antwort2 = $('input[id="antwort2"]').prop();
antwort3 = $('input[id="antwort3"]').prop();

Fiddle

You could do like this:

var antwort1 = $('#antwort1:checked').length;
var antwort2 = $('#antwort2:checked').length;

console.log(antwort1);
console.log(antwort2);

FIDDLE DEMO

Post a comment

comment list (0)

  1. No comments so far