如何在加载时通​​过jQuery选择一个单选按钮?

时间:2016-04-10 08:57:58

标签: javascript jquery html



$("#1").find("Radio1").prop('checked', true);
$("#2").find("Radio1").prop('checked', true);

<div id="1" cvalue='false'>
  Are you a student  ?
  <br> <input type="radio" name="RadioB1" id="Radio1" value="1">
  <label for="Radio1">Yes</label><br>
  <input type="radio" name="RadioB1" id="Radio2" value="2">
  <label for="Radio2">No</label>
</div>
<br/>
<div id="2"  cvalue='false' >
  Do you study in a university ?
  <br>
  <input type="radio" name="RadioB1" id="Radio3" class="ui-state-default ui-corner-all" value="1">
  <label for="Radio1">Yes</label><br>
  <input type="radio" name="RadioB1" id="Radio4" class="ui-state-default ui-corner-all" value="2">
  <label for="Radio2">No</label>
</div>
&#13;
&#13;
&#13;

DEMO

1 个答案:

答案 0 :(得分:0)

我想知道您为什么要使用javascript来执行此操作?你可以像这样使用纯html

<div id="1" cvalue='false'>
  Are you a student  ?
  <br> <input type="radio" name="RadioB2" id="Radio1" value="1" checked="checked">
  <label for="Radio1">Yes</label><br>
  <input type="radio" name="RadioB2" id="Radio2" value="2">
  <label for="Radio2">No</label>
</div>
<br/>
<div id="2"  cvalue='false' >
  Do you study in a university ?
  <br>
  <input type="radio" name="RadioB1" id="Radio3" class="ui-state-default ui-corner-all" value="1" checked="checked">
  <label for="Radio1">Yes</label><br>
  <input type="radio" name="RadioB1" id="Radio4" class="ui-state-default ui-corner-all" value="2">
  <label for="Radio2">No</label>
</div>

注意: 您只需使用checked="checked"并使用不同的name为每个问题选择多个。