Get the App
SLTechnology News&Howtos  ›  Development  › 

How does jQuery get the value of the select drop-down box

Shulou Source: shulou.com Published: 2022-06-01 02:10:12 09月30日 Update

This article mainly introduces the relevant knowledge of "jQuery how to obtain the value of the select drop-down box". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope that this article "how to obtain the value of the select drop-down box by jQuery" can help you solve the problem.

Several scenarios for getting the select drop-down box are as follows:

1. Get the value of the first option

('# test option:first'). Val ()

two。 Value of the last option

('# test option:last'). Val ()

3. Get the value of the second option

('# test option:eq (1)'). Val ()

The values of the third and fourth option can be obtained by analogy

4. Get the selected value

Var groupid = $("# groupid"). Find ("option:checked"). Val (); $('# groupidoption:selected'). Val (); $('# groupid'). Val ()

5. Set option to be selected with a value of 2

$('# test'). Attr ('value','2')

6. Set the last option to be 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 select

$('# test option') .length

8. Add an option

Var str= "item N + 1"; $("# test") .append (str); / / this is generally used to append $("item N + 1") .appendTo ("# test")

9. Delete selected item

('# test option:selected'). Remove ()

10. Delete the first item in the selection

('# test option:first'). Remove ()

11. Delete an option that meets the criteria

$('# test option') .each (function () {if ($(this). Val () ='5') {$(this). Remove ();}}); $('# test option [value=5]'). Remove (); this is the end of the content about "how jQuery gets the value of the select drop-down box". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Drop-down knowledge industry different practical three content practicality reality situation article method more condition case status knowledge point article information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft Shulou Technology Huawei Apple