jquery Radio value, checkbox value, select Value, Radio selected, CheckBox selected, select selected, and its related

Source: Internet
Author: User

1. JQuery gets the Text and Value selected by select:

$("#select_id"). Change (function () {//code ...}); //Add an event for Select, which is triggered when one of the items is selected varchecktext=$ ("#select_id"). Find ("option:selected"). text ();//gets the text selected by the Select varcheckvalue=$ ("#select_id"). Val ();//gets the value selected by Select varcheckindex=$ ("#select_id").Get(0). SelectedIndex;//gets the index value of the select selection varmaxindex=$ ("#select_id Option:last"). attr ("Index");//Gets the maximum index value of the Select

2. jquery Add/Remove option for select:

$ ("#select_id"). Append ("<option value= ' value ' >Text</option>");//append an option to the Select ( drop-down item) $("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>");//Insert an option for select (first position) $("#select_id option:last"). Remove ();//Delete index value in select maximum option (last) $("#select_id option[index= ' 0 ']"). Remove ();//Delete option with index value 0 in select (first) $("#select_id option[value= ' 3 ']"). Remove ();//Delete the optiona of value= ' 3 ' in select $("#select_id option[text= ' 4 ']"). Remove ();//Delete the optiona of text= ' 4 ' in select

3. Select option Empty:

$ ("#charCity"). empty ();

<select class= "selector" ></select>

1. Set value to Pxx item selected

$ (". Selector"). Val ("Pxx");

2. Set text to Pxx to select

$ (". Selector"). Find ("option[text= ' Pxx ')"). attr ("selected",true);

Here is the use of a bracket, preceded by the attribute name in parentheses, without quotation marks. In many cases, the use of brackets can make logic very simple.

3. Gets the value of the currently selected item

$ (". Selector"). Val ();

4. Gets the text of the currently selected item

$ (". Selector"). Find ("option:selected"). Text ();

Using a colon here, mastering its usage and extrapolate will also make the code concise.

There are many times when a select cascade is used, that is, the value of the second select changes with the value selected in the first select. This is very simple in jquery.

Such as:

$ (". Selector1"). Change (function() {     //  Empty the second      $ (". Selector2") first. Empty ();      // in real-world applications, the option here is typically generated by looping multiple      var option = $ ("<option>"). Val (1). Text ("Pxx");      $ (". Selector2"). Append (option);});

jquery sets the text and value selected for select:
Syntax Explanation:

$ ("#select_id"). Get (0). selectedindex=1;  // Set the Select index value to 1 to select $ ("#select_id"). Val (4);   // Sets the value of select to 4 for the item selected true);   // set the text value of select to the item selected for jquery

1. Gets the value of a set of radio selected items


2. Get the text of select selected item


3. The second element of the Select drop-down box is the current selected value

4, radio the second element of the radio group is the currently selected value


Get Value:

1. text box, text area

$ ("#txt"). attr ("value");

2. Multiple marquee checkbox

$ ("#checkbox_id"). attr ("value");

3, single-selection group radio

4. Drop-down box select

Control form elements:

1. text box, text area

$ ("#txt"). attr ("Value", "'); //
$ ("#txt"). attr ("value", ' 11 '); //

2. Multiple marquee checkbox

$ ("#chk1"). attr ("Checked", "'); //  $ ("#chk2"). attr ("Checked",true); //  If//

3, single-selection group radio

("Input[type=radio]"). attr ("Checked", ' 2 '); //

4. Drop-down box select

$ ("#sel"). attr ("value", '-sel3 '); //
$ ("<option value= ' 1 ' >1111</option><option value= ' 2 ' >2222</option>"). AppendTo ("#sel")  $ ("#sel"). empty ();// empty drop-down box 

jquery Radio value, checkbox value, select Value, Radio selected, CheckBox selected, select selected, and its related

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.