jQuery throwing error when trying to get selected radio button value
Given this radio button set:
<div id="reviewScope">
<input type="radio" name="loadMarkers" id="day" value="day"><label for="day">24 Hours</label>
<input type="radio" name="loadMarkers" id="week" value="week"><label for="week">Week</label>
<input type="radio" name="loadMarkers" id="month" value"month"><label for="month">Month</label>
</div>
Then calling using jQuer开发者_如何转开发y 1.5.2, I'm trying to get the value of the selected radio button following the answer here:
$('#reviewScope input:radio').change(function() {
period = $("input[name='loadMarkers']:checked").val();
....
}
Firebug is throwing the error:
$("input[name='loadMarkers']:checked").val is not a function
What am I doing wrong?
Are you missing the closing ')' in the change method?
$('#reviewScope input:radio').change(function() {
period = $("input[name='loadMarkers']:checked").val();
....
})
http://jsfiddle.net/67Q68/
You can do this:
$('#reviewScope input:radio').each(function() {
$(this).click(function() {
period = $("input[name='loadMarkers']:checked").val();
....
});
});
Check for closing parenthesis and brackets
$("#day").click(function() {
if ($(this).is(':checked')) {
var period = "day";
}
});
Do the same as for other radio buttons one by one
精彩评论