如何获得收音机的价值?

时间:2017-04-13 05:59:40

标签: jquery html

HTML:

<div id="chips">
    <label>
        <input type="radio" name="pet_chipped" class="chipped" value="Yes" id="chipyes" checked> Yes</label>
    <label>
        <input type="radio" name="pet_chipped" class="chipped" value="No" id="chipno"> No</label>
</div>

jQuery的:

$("input[name='pet_chipped']:checked").val();

当我发出警报时,小时候会发出警告。它显示未定义。

2 个答案:

答案 0 :(得分:2)

&#13;
&#13;
//onload event
$(document).ready(function(){
  /*show alert on load time*/
  alert($('[name="pet_chipped"]:checked').val());
})
// on change radio button value fire event 
$(document).on('change', '[name="pet_chipped"]', function(){
  //show value of radio after changed
   alert($('[name="pet_chipped"]:checked').val());
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="chips">
<label> <input type="radio" name="pet_chipped" class="chipped" value="Yes" id="chipyes" checked> Yes</label>
<label><input type="radio" name="pet_chipped" class="chipped" value="No" id="chipno"> No</label>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个

HTML:

<input type="radio" name="pet_chipped" class="chipped" value="Yes">    Yes</label>
<label>
<input type="radio" name="pet_chipped" class="chipped" value="No" checked> No</label>
<input type="submit" onclick="getChecked()" />

Jquery:

<script type="text/javascript">
function getChecked(){
var checked = $("input[name='pet_chipped']:checked").val();
alert(checked);
}
</script>