jQuery operation selection drop-down box select value

js and jQuery joint operations dom is actually pretty good, if not a professional front-end staff, I feel it as long as the front end language proficiency js and jQuery on it.
Get down box select several situations as follows:

1. Obtain the first value of the option

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

2. The value of the last option of

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

3. Get the value of the second option

$('#test option:eq(1)').val();
依次类推可以获取第三个、第四个option的值

4. Get the selected value

var groupid = $("#groupid").find("option:checked").val();
$('#groupidoption:selected').val();
$('#groupid').val();
<td align="center" class="tableFormLabel" >
    <select id="groupid" class="input-text" >  </select>
</td>

The option is set to 2 selected

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

6. Set the last option is selected

$('#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. The length of the select Get

$('#test option').length;

8. Add a option

$("#test").append("<option value='n+1'>第N+1项</option>"); //一般都用这个追加

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

9. Delete selected items

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

10. The first item to delete the selected item

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

11. Delete option to meet the conditions

$('#test option').each(function(){
   if( $(this).val() == '5'){
        $(this).remove();
    }
});

$('#test option[value=5]').remove();

Reference blog: https://www.cnblogs.com/eager/p/7133270.html

Guess you like

Origin www.cnblogs.com/jasonboren/p/12091682.html