Jquery drop-down box (DropDownList) value assignment code (Wang Huan)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Jquery drop-down box (DropDownList) value assignment code (Wang Huan)

1 get the selected item:

  1. Get the selected item Value:
  2. $('select#sel option:selected').val();
  3. Or
  4. $('select#sel').find('option:selected').val();
  5. Get the selected item Text:
  6. $('select#seloption:selected').text();
  7. Or
  8. $('select#sel').find('option:selected').text();

2 for the currently selected item index value:

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

The maximum index 3 to obtain the current value of option:

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

4 for the length of the DropdownList:

  1. $('select#sel')[0].options.length;
  2. Or
  3. $('select#sel').get(0).options.length;

5 set up the first option for the selected value:

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

6 set the last option for the selected value:

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

7 according to the index value is set an arbitrary option for the selected value:

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

8 Value=4 option for the selected value:

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

Remove Value=3 option 9:

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

10 remove several option:

  1. $(" select#sel option ").eq (index value).Remove (); =0 value index,1,2....
  2. Such as the deletion of third Radio:
  3. $(" select#sel option ").eq(2).remove();

11 delete the first option:

  1. $(" select#sel option ").eq(0).remove();
  2. Or
  3. $("select#sel option:first").remove();

12 remove the last option:

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

Remove the 13 dropdownlist:

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

14 in the select add a option:

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

15 in front of the select to add a option:

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

16 option traversal:

  1. $(' select#sel option ').each(function (index, domEle) {
  2. //Write code
  3. });

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ellis at November 17, 2013 - 10:54 AM