简体   繁体   中英

Get value from radio button

I'm trying to make a multiple select radio but the output to the console is console=on.

That doesnt help at all. I need to know which (none,t,i or ti) that is selected.

Any idea how to do this?

 $('#btnSubmit').click(function() { var data = $('#containerCreationForm').serialize(); console.log(data); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="containerCreationForm" method="post"> <div class="content-form"> <label class="col-sm-2 control-label">Console</label> <div class="col-sm-10"> <div class="col-sm-5"> <div class="radio radio-info"> <input name="console" id="it" class="form-control" type="radio"> <label for="it">Interactive &amp; TTY <span class="text-blue">(-i -t)</span> </label> </div> <div class="radio radio-info"> <input name="console" id="tty" class="form-control" type="radio"> <label for="tty">TTY <span class="text-blue">(-t)</span> </label> </div> </div> <div class="col-sm-5"> <div class="radio radio-info"> <input name="console" id="interactive" class="form-control" type="radio"> <label for="interactive">Interactive <span class="text-blue">(-i)</span> </label> </div> <div class="radio radio-info"> <input name="console" id="none" class="form-control" type="radio" checked=""> <label for="none">None</label> </div> </div> </div> <button type="button" id="#btnSubmit">Submit</button> </div> 

You need to use names and values to identify what your radios mean eg

 $(function() { $("form").submit( function(args) { args.preventDefault(); console.log($(this).serialize()); } ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <fieldset> <legend>Gender:</legend> Male: <input type="radio" name=gender value=m> Female: <input type="radio" name=gender value=f> Other <input type="radio" name=gender value=o> </fieldset> <input type=submit> </form> 

this will result in a form value of gender= m | f | o

also note you should be using the forms submit event as this will invoke the forms validation logic which your current code is bypassing

Add value attribute in radio button.

Example: Since,you are using Jquery in your code. I'm using it here:

$(document).ready(function(){
        $("input[type='button']").click(function(){
            var radioValue = $("input[name='gender']:checked").val();
            if(radioValue){
                alert("Your are a - " + radioValue);
            }
        });

    });

In HTML:

<label><input type="radio" name="gender" value="male">Male</label> 
<label><input type="radio" name="gender" value="female">Female</label>

    <p><input type="button" value="Radio Value"></p>

Hope it helps..!

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