Home  >  Article  >  Web Front-end  >  Jquery operates drop-down box (DropDownList) to implement value assignment_jquery

Jquery operates drop-down box (DropDownList) to implement value assignment_jquery

WBOY
WBOYOriginal
2016-05-16 17:25:421811browse

1. Get the selected item:

Copy code The code is as follows:

Get the Value of the selected item :
$('select#sel option:selected').val();
or
$('select#sel').find('option:selected').val();
Get the Text value of the selected item:
$('select#seloption:selected').text();
or
$('select#sel').find('option:selected' ).text();

2. Get the index value of the currently selected item:
Copy code The code is as follows:

$('select#sel').get(0).selectedIndex;

3. Get the maximum index value of the current option:
Copy code The code is as follows:

$('select#sel option:last').attr( "index")

4. Get the length of DropdownList:
Copy code The code is as follows:

$('select#sel')[0].options.length;
or
$('select#sel').get(0).options.length;

5. Set the first option to the selected value:
Copy the code The code is as follows:

$('select#sel option:first').attr('selected','true')
or
$('select#sel')[0].selectedIndex = 0;

6. Set the last option to the selected value:
Copy code The code is as follows:

$('select#sel option:last).attr('selected','true')

7. Set any option to be selected based on the index value Value:
Copy code The code is as follows:

$('select#sel')[0 ].selectedIndex = index value; index value = 0,1,2....

8. Set the option of Value=4 to the selected value:
Copy code The code is as follows:

$('select#sel').attr('value','4');
Or
$("select#sel option[value='4']").attr('selected', 'true');

9. Delete option with Value=3 :
Copy code The code is as follows:

$("select#sel option[value=' 3']").remove();

10. Delete which option:
Copy code The code is as follows:

$(" select#sel option ").eq(index value).remove(); index value=0,1,2....
For example, delete the third Radio:
$(" select#sel option ").eq(2).remove();

11. Delete the first option:
Copy code The code is as follows:

$(" select#sel option ").eq(0).remove ();
or
$("select#sel option:first").remove();

12. Delete the last option:
Copy code The code is as follows:

$("select#sel option:last").remove();

13. Delete dropdownlist:
Copy code The code is as follows:

$( "select#sel").remove();

14. Add an option after select:
Copy code The code is as follows:

$("select#sel").append("f");

15. Add an option in front of select:
Copy code The code is as follows:

$("select#sel").prepend("0");

16. Traverse options:
Copy code The code is as follows:

$(' select #sel option ').each(function (index, domEle) {
//Write code
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn