jQuery当前检查单选按钮值显示始终第一个按钮值

时间:2013-12-09 09:23:03

标签: javascript jquery radio checked

我有一个如下的javascript:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<li>
<input type="radio" class="gal_location" name="gal_location" value="0"> All
</li>
<li>
<input type="radio" name="gal_location" class="gal_location" checked="checked" value="11">Sivasagar</li>
<li>
<input type="radio" name="gal_location" class="gal_location" value="3">Guwahati</li>
<li><input type="radio" name="gal_location" class="gal_location" value="9">Jorhat</li>

<script>                        
if($('.gal_location').is(':checked')) { 
    var location_id = $("input:radio[name=gal_location]").val();     
}
alert('Location id is '+location_id);

</script>

现在我希望收到Location id is 11的提醒,但我会收到Location id is 0的提醒

Code sample JSFiddle

1 个答案:

答案 0 :(得分:6)

使用:checked获取checkradio按钮值

var location_id = $("input:radio[name=gal_location]:checked").val()

DEMO