如何用jQuery获得select的值,最近一直在写jsp,好久不写jq
1.获取第一个option的值
$('#test option:first').val();
2.最后一个option的值
$('#test option:last').val();
3.获取第二个option的值
$('#test option:eq(1)').val();
4.获取选中的值
$('#test').val();
$('#test option:selected').val();
5.设置值为2的option为选中状态
$('#test').attr('value','2');
6.设置最后一个option为选中
$('#test option:last').attr('selected','selected');
$("#test").attr('value' , $('#test option:last').val());
$("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val());
7.获取select的长度
$('#test option').length;
8.添加一个option
$("#test").append("<option value='n+1'>第N+1项</option>");
$("<option value='n+1'>第N&
jq 关于select的下拉选中
最新推荐文章于 2023-10-06 22:18:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)