How does jq get the value of the selected option_use jquery to operate select (get the value of the selected option, etc.)

$('#ITEM_CODE option:selected').text() Gets the selected text value. not value

Summarize the method of using jQuery to operate select.

1. Get the value of the first candidate.

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

2. Get the value of the last candidate.

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

3. Get the value of the second candidate.

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

4. Get the value of the selected candidate.

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

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

5. Set the candidate with a value of 2 as selected.

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

6. Set the last candidate to 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. Get the length of the candidate list.

$('#test option').length;

8. Add a candidate.

$("#test").append("N+1 item");

$("N+1 item").appendTo("#test");

9. Delete the selected item.

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

10. Delete item (the first item is deleted here).

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

11. Delete the specified value.

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

$(this).remove();

}

});

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

12. Get the label of the first group.

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

13. Get the value of the first candidate under the second group.

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

14. Select the candidate according to the value of the candidate.

$("#sel option:contains('C')").prop("selected", true);

Select is used a lot in business forms. It is necessary to master how to use jQuery to operate select, even though the current front-end development trend is to advocate operating data and avoid directly operating dom (such as vue).


 

Guess you like

Origin blog.csdn.net/qq_44625745/article/details/129385058