如何在加载时通过jQuery选择单选按钮
How to select a radio button through jQuery on load?
$("#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>
演示
首先,两个单选类按钮的名称应该保持不同,以便从两个div中进行选择。
第二,你正在寻找标签并设置其道具,相反,你应该找到输入单选按钮为:
HTML:
<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>
JS:
$('#1').find('input[name=RadioB1]').filter('#Radio1').prop('checked', true);
$('#2').find('input[name=RadioB2]').filter('#Radio3').prop('checked', true);
请参阅此演示
我想知道为什么要使用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
为每个问题选择多个
相关文章:
- 如何使用jQuery选择下拉列表的值
- 按照选项卡索引的顺序循环一个jQuery选择
- 使用Jquery选择或取消选择ListView中的所有复选框
- jQuery选择器无法正常工作
- 使用基于文本的 Jquery 选择选项
- 将jquery选择器转换为数组
- JavaScriptDOM正常工作时JQuery选择器不工作
- 在我的情况下,使用带有变量失败的 jquery 选择器
- 换行符插入jquery选择器
- 使用javascript"这个“;用于jquery选择器
- jQuery选择器缓存问题
- jQuery 选择器,所有跨度都在 DIV 内
- JQuery选择器:如果同级具有.class,则选择td
- jQuery选择器不识别任何动态创建的HTML输入函数
- 循环遍历元素jquery选择器
- jQuery选择器错误:无法识别的表达式
- 自定义jquery选择器属性未在模拟器中激发
- Jquery选择器为空,我应该使用哪个作用域
- jQuery选择JavaScript生成的元素
- Jquery选择菜单选项在页面刷新时失败