JQuery gets and sets the value implementation code for the select drop down box

  • 2020-03-26 23:48:43
  • OfStack

For the Select:

Get the text selected by select:

$(" # ddlRegType "). The find (" option: selected "). The text ();


Get the value selected by the select:

$(" # ddlRegType "). Val ();


Gets the index selected by select:

$(" # ddlRegType "). The get (0). SelectedIndex;


Set up the select:

Set the index selected by select:

$(" # ddlRegType "). The get (0). SelectedIndex = index; //index is the index value


Set the selected value of select:

      $(" # ddlRegType "). Attr (" value ", "Normal");

      $(" # ddlRegType "). Val (" Normal ");

      $(" # ddlRegType "). The get (0). The value = value;


Set the text selected by select:

Var count = $(" # ddlRegType option "). The length;

  For (var I = 0; I< The count. I++)  
        {                    If ($(" # ddlRegType "). The get (0). The options [I] text = = text)  
              { 
                      $(" # ddlRegType "). The get (0). The options [I]. Selected = true;  

                      Break;  
              }  
      }

$(" # select_id option [text = 'jQuery'] "). The attr (" selected ", true);


Set the select option item:

  $(" # select_id "). Append (" < The option value = 'value' > Text< / option>" );   // add an option

  $(" # select_id "). The prepend (" < The option value = '0' > Please select < / option>" ); // insert an option in front of it

  $(" # select_id option: last "). The remove (); // deletes the Option with the largest index value

  $(" # select_id option [index = '0'] "). The remove (); // deletes Option with index value of 0

  $(" # select_id option (value = '3'] "). The remove (); // deletes an Option with a value of 3

  $(" # select_id option [text = '4'] "). The remove (); // delete Option with TEXT value of 4
 

To empty the Select:

$(" # ddlRegType "). The empty ();


Related articles: