jquery Gets and sets the select selected value/text/index and selected events

Source: Internet
Author: User
Get checked items gets the value of the selected item
$ ("#select1"). Val ();
gets the text of the selected item
$ ("#select1 option:selected"). Text ();
or
$ ("#select1"). Find ("option:selected"). Text ();
gets the index of the selected item
$ ("#select1"). Get (0). SelectedIndex;
Set the selection Select by Value
The option to set a value of 2 selects
$ ("#select1"). Val (2);
Select by text
Set Text to test option selected
//This is the wrong wording
$ ("#select1 option[text= ' test ']"). attr ("selected", true);
Input supports this way to get a property value: "Input[text= ' Test '"
//Select Requires "option:contains (' Test ')" to get
//correctly written
$ ("# Select1 option:contains (' Test ') "). attr (" selected ", true);
or
$ ("#select1"). Find ("option:contains (' Test ')"). attr ("selected", true)
Follow index to select
Select the third item
$ ("#select1"). Get (0). selectedindex=2;
Select Select Event Change, not click
$ ("#select1"). Change (function () {
    Console.log (the selected value is: + $ (this). Val ());
});
Add/Remove option items for select
$ ("#select_id"). Append ("<option value= ' value ' >Text</option>");  Appends an option (Drop-down)
$ ("#select_id") to the Select. Prepend ("<option value= ' 0 ' > Please select </option>");  Inserts an option (first position)
$ ("#select_id option:last") for the Select. Remove ();  Deletes the maximum index value option (the last)
$ ("#select_id option[index= ' 0 ']") of the Select. Remove ();  Deletes option (first)
$ ("#select_id option[value= ' 3 ']" with index value of 0 in the Select. Remove ();  Delete option
$ ("#select_id option[text= ' 4 ') of value= ' 3" in the Select). Remove ();  Delete option $ ("#select_id") for text= ' 4 ' in the Select
. empty ();/Clear dropdown box
get and set the radio selected value for the Radio box
Gets the value of a set of radio selected Items
= $ (' input[name=items][checked] '). Val ();  

The second element of the radio Radio Group is the currently selected value
$ (' input[name=items] '). Get (1). checked = true;
The item to set VALUE=2 is currently selected 
$ ("Input[type=radio]"). attr ("Checked", ' 2 ');
get and set multiple-selection box check values
$ ("#chk1"). attr ("Checked", ""); Do not tick
$ ("#chk2"). attr ("Checked", true);//Tick
if ($ ("#chk1"). attr (' checked ') ==undefined)//judge whether it has been ticked
Reference

Http://www.cnblogs.com/zhangym118/p/5509673.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.