jquery中select的用法
时间: 2023-10-03 12:08:49 浏览: 50
jQuery中可以通过`$('select')`来获取所有的`<select>`元素,然后可以使用一系列方法来操作它们。
常用的select方法有:
1. `val()`: 获取或设置`<select>`元素的选中值。
例如:`$('select').val()`获取选中的值,`$('select').val('value')`将`<select>`选中值设置为`value`。
2. `change()`: 触发`<select>`元素的`change`事件。
例如:`$('select').change()`触发`<select>`的`change`事件。
3. `prop()`: 获取或设置`<select>`元素的属性。
例如:`$('select').prop('disabled', true)`将`<select>`设置为不可用。
4. `removeAttr()`: 移除`<select>`元素的属性。
例如:`$('select').removeAttr('disabled')`移除`<select>`的`disabled`属性。
5. `each()`: 遍历所有的`<select>`元素。
例如:`$('select').each(function() {console.log($(this).val())})`遍历所有`<select>`元素并输出它们的选中值。
还有其他的方法可以用来操作`<select>`元素,可以根据实际需求选择使用。
相关推荐
![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://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![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://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)