简体   繁体   中英

Getting Radio Buttons to output info

I am very new to coding and CS and so sorry if this question is kind of obvious. I couldn't really find anything about this before that I understood. I am making a set of radio buttons and I would like the user to check off what applies to them and submit it. How do I get the value of the button (with javascript) that they click so that my code could generate a corresponding output.

        <p> Pick a hair length </p>

        <input type="hidden" value="" id="rdValue" />

        <div>
            <label> <input type="radio" name="length" value="short"> Short </label>
        </div>
        <div>
            <label> <input type="radio" name="length" value="med"> Medium </label>
        </div>
        <div>
            <label><input type="radio" name="length" value="long"> Long </label>
        </div>

        <p> Pick a hair type </p>

        <div>
            <label> <input type="radio" name="type" value="curly"> Curly </label>
        </div>
        <div>
            <label> <input type="radio" name="type" value="wavy"> Wavy </label>
        </div>
        <div>
            <label><input type="radio" name="type" value="straight"> Straight </label>
        </div>

You could use this to get the value for the selected radio button from the "length" radio buttons for example:

$("input:radio[name='length']").val();

This question is very similar to yours, and has answers in jQuery and Javascript. It's pretty simple.

Basically, you get the DOM elements of the radio buttons from the page, then loop through them checking their values. Once you find one that is "true", you've found the selected value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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