以下是一些使用jQuery操作radio的技巧:
- 设置选中的radio按钮:使用
prop()
方法将radio按钮的checked
属性设置为true
。
$('input[name="radioName"]').eq(0).prop('checked', true);
- 获取选中的radio按钮的值:使用
val()
方法获取选中的radio按钮的值。
var selectedValue = https://www.yisu.com/ask/$('input[name="radioName"]:checked').val();
- 监听radio按钮的改变事件:使用
change()
方法监听radio按钮的改变事件。
$('input[name="radioName"]').change(function() { // 在这里执行操作 });
- 禁用/启用radio按钮:使用
prop()
方法将radio按钮的disabled
属性设置为true
(禁用)或false
(启用)。
$('input[name="radioName"]').eq(0).prop('disabled', true);
- 获取所有的radio按钮:使用选择器
input[type="radio"]
获取所有的radio按钮。
var radioButtons = $('input[type="radio"]');
- 根据值选中radio按钮:使用
val()
方法和选择器选择具有特定值的radio按钮,并将其设置为选中状态。
$('input[name="radioName"][value="https://www.yisu.com/ask/valueToSelect"]').prop('checked', true);
希望以上技巧能够帮到您!