如何用jQuery得到select的值

1.獲取第一個option的值       app

 $('#test option:first').val();

 

2.最後一個option的值                     this

$('#test option:last').val();

 

3.獲取第二個option的值          spa

$('#test option:eq(1)').val();

 

4.獲取選中的值                         code

$('#test').val();

$('#test option:selected').val();

 

5.設置值爲2的option爲選中狀態   blog

$('#test').attr('value','2');

 

6.設置最後一個option爲選中rem

$('#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的長度            it

$('#test option').length;

 

8.添加一個optionio

$("#test").append("<option value='n+1'>第N+1項</option>");

$("<option value='n+1'>第N+1項</option>").appendTo("#test");

 

9.添除選中項       ast

 $('#test option:selected').remove();

 

10.刪除項選中(這裏刪除第一項)       function

$('#test option:first').remove();

 

11.指定值被刪除

複製代碼
$('#test option').each(function(){

   if( $(this).val() == '5'){

        $(this).remove();
    }
});

$('#test option[value=5]').remove();
複製代碼

 

12.獲取第一個Group的標籤

$('#test optgroup:eq(0)').attr('label');

 

13.獲取第二group下面第一個option的值

$('#test optgroup:eq(1) : option:eq(0)').val();

 

14.根據option的值選中option

$("#sel option:contains('C')").prop("selected", true);
相關文章
相關標籤/搜索