简体   繁体   中英

jQuery: can't get radio button value

I have the following jsfiddle:

http://jsfiddle.net/rpp6s/

I want to alert the value of the second radio button form. But if I select a value from the first form, it's value appears in the alert instead of the second form's value.

html:

<form id="formOne">
New invoice <input id="radioNewInvoice" name="groupOne" type="radio" value="new"></input>
 Modify existing invoice<input id="radioModInvoice" name="groupOne" type="radio" value="mod"></input>
</form>



<form id="form2">
    <input id="normal" type="radio" name="groupTwo" value="normal" /> normal <br>
    <input id="30days" type="radio" name="groupTwo" value="30days" /> 30 days <br>
    <input id="60days" type="radio" name="groupTwo" value="60days" /> 60 days <br>
    <input id="90days" type="radio" name="groupTwo" value="90days" /> 90 days <br>
    <input id="90pdays" type="radio" name="groupTwo" value="90+days" /> 90+ days <br>
</form>

<input id="but" type="button" value="click" />

javascript:

$('#but').click(function() {
    alert($("input[@name=groupTwo]:checked").val());
});

Rmove the @ from the input selector.

$('#but').click(function(){
   alert($("input[name=groupTwo]:checked").val()); 
});

use this:

alert($('input[name=groupTwo]:checked').val());

or to get a specific form:

alert($('input[name=groupTwo]:checked', '#form2').val());
$("#but").click(function() {
    alert($("#form2 input:checked").val());
});
$('#but').click(function(){
   alert($("#form2 input[@name=groupTwo]:checked").val()); 
});
alert($("input[name=groupTwo]:checked").val()); 
$('#but').click(function(){
      alert($("input[name*='groupTwo']:checked").val());
});

Change your alert to

alert($("#form2 input:checked").val()); 

It's the quick and dirty solution, but it works.

 alert($('input[name=groupTwo]:radio:checked').val());

这很好用:D

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