jquery实现动态获取值赋值给select设置默认值
时间: 2023-08-31 22:15:39 浏览: 59
假设你有一个下拉框(select)如下:
```html
<select id="mySelect">
<option value=""></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
```
通过jQuery可以动态获取值,然后将其赋值给该下拉框作为默认值。例如,如果你想将默认值设置为2,则可以使用以下代码:
```javascript
$(document).ready(function(){
$('#mySelect').val('2');
});
```
这将在文档加载完成后执行,将下拉框的默认值设置为“Option 2”。
如果你想从一个文本框中获取值并将其设置为下拉框的默认值,则可以使用以下代码:
```javascript
$(document).ready(function(){
var myValue = $('#myTextBox').val();
$('#mySelect').val(myValue);
});
```
这将从ID为“myTextBox”的文本框中获取值,并将其设置为下拉框的默认值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)