如何获取选定的单选按钮值?

时间:2016-05-19 04:39:26

标签: jquery html radio-button

我需要获取所选的单选按钮值。单选按钮名称为is_external

的test.html

<div class="exter_inter">
    External <input type="radio" name="is_external" value="1" <?php if(isset($_GET['status']) && $_GET['status']=='1') echo "checked";?> />
    Internal <input type="radio" name="is_external" value="0"  <?php if(isset($_GET['status']) && $_GET['status']=='0') echo "checked";?>  />
</div>

的script.js

$(document).ready(function() {  
    $('input:radio').change(function() {      
        var is_external = $("input[name='radio']:checked").val();   
        alert("selected values: " + is_external); //Return undefined  

    });
} ); 

2 个答案:

答案 0 :(得分:3)

$(document).ready(function() {
  $('input:radio').change(function() {
    var is_external = $("input[name='is_external']:checked").val();//name is is_external not radio
    alert("selected values: " + is_external); //Return undefined  

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="exter_inter">
  External
  <input type="radio" name="is_external" value="1" />Internal
  <input type="radio" name="is_external" value="0" />
</div>

名称为is_external而不是radio

答案 1 :(得分:0)

   <input type="radio" name="user" value="newUser">New User
   <input type="radio" name="user" value="registeredUser">Registered User

    $(document).ready(function () {
        $("input:radio").change(function () {
            var x = $("input[name='user']:checked").val();
            console.log(x);
         });
    });