让一个单选框默认选中 jquery
时间: 2023-09-22 08:08:22 浏览: 41
可以使用jQuery来让一个单选框默认选中。可以使用`prop()`方法来设置`checked`属性的值为`true`。例如,如果该单选框的id为"myRadio",可以这样写:
```javascript
$("#myRadio").prop("checked", true);
```
这样就将该单选框默认选中了。需要注意的是,使用`prop()`方法设置`checked`属性时,需要将属性值设置为布尔类型的`true`,而不是字符串类型的`"true"`。
如果需要在用户加载页面时自动选中该单选框,可以将上述代码放在文档加载完成后的事件处理函数中。例如:
```javascript
$(document).ready(function() {
$("#myRadio").prop("checked", true);
});
```
这样当文档加载完成后,就会自动选中该单选框了。
相关问题
uniapp单选框默认选中第一个
可以通过给第一个单选框加上 checked 属性来默认选中第一个选项。例如:
<template>
<div>
<label><input type="radio" name="fruit" value="apple" checked> 苹果</label>
<label><input type="radio" name="fruit" value="banana"> 香蕉</label>
<label><input type="radio" name="fruit" value="orange"> 橙子</label>
</div>
</template>
jquery 单选框默认被选中
使用 jQuery 设置单选框的默认选中项可以通过设置 `checked` 属性来实现。例如,如果你的 HTML 代码如下:
```html
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
```
你可以通过以下 jQuery 代码将 Male 选项设置为默认选中项:
```javascript
$(document).ready(function() {
$('input[name="gender"][value="male"]').prop('checked', true);
});
```
这里我们使用了 `prop()` 方法设置 `checked` 属性为 `true`。注意,我们使用了 `name` 属性来区分不同的单选框,而使用 `value` 属性来选择要设置为默认选中项的单选框。