JQuery obtains the Text and Value selected by the Select statement.

Source: Internet
Author: User

JQuery obtains the Text and Value selected by the Select statement.
JQuery gets the selected Text and Value of Select:

Syntax explanation:
1. $ (# select_id). change (function () {// code...}); // Add an event for Select, triggered when one of the Select
2. var checkText = $ (# select_id). find (option: selected). text (); // obtain the selected Text
3. var checkValue = $ (# select_id). val (); // obtain the Value selected by Select
4. var checkIndex = $ (# select_id). get (0). selectedIndex; // obtain the index value selected by Select
5. var maxIndex = $ (# select_id option: last). attr (index); // obtain the maximum index value of Select.
JQuery sets the Text and Value selected by Select:
Syntax explanation:
1. $ (# select_id). get (0). selectedIndex = 1; // Select an item whose Select index value is 1
2. $ (# select_id). val (4); // Select an item whose Value is set to 4.
3. $ (# select_id option [text = 'jquery ']). attr (selected, true); // set the Select Text value to the selected items of jQuery.

JQuery adds/deletes Select Option items:
Syntax explanation:
1. $ (# select_id). append (

Text); // Append an Option to Select (drop-down)
2. $ (# select_id). prepend ( Select); // Insert an Option for Select (first position)
3. $ (# select_id option: last). remove (); // Delete the largest Option (last) of the index values in the Select statement)
4. $ (# select_id option [index = '0']). remove (); // Delete the Option with the index value 0 in Select (the first option)
5. $ (# select_id option [value = '3']). remove (); // Delete the Option with Value = '3' in Select.
5. $ (# select_id option [text = '4']). remove (); // Delete the Option of '4' Text = '4' in Select.

Www.bkjia.com

Jquery radio, checkbox, select, radio, checkbox, select, and related
Obtains the value of a set of radio selected items.
Var item = $ ('input [name = items] [checked] '). val ();
Text of the selected select item
Var item = $ (select [name = items] option [selected]). text ();
The second element in the select drop-down box is the selected value.
$ ('# Select_id') [0]. selectedIndex = 1;
The second element of the radio Group is the currently selected value.
$ ('Input [name = items] '). get (1). checked = true;
Get value:
Text Box, text area: $ (# txt). attr (value );
Multiple selection box checkbox: $ (# checkbox_id). attr (value );
Radio Group radio: $ (input [type = radio] [checked]). val ();
Drop-down box select: $ ('# sel'). val ();
Control form elements:
Text Box, text area: $ (# txt). attr (value, ''); // clear the content
$ (# Txt). attr (value, '11'); // fill in the content
Multiple choice box checkbox: $ (# chk1). attr (checked, ''); // do not check
$ (# Chk2). attr (checked, true); // check
If ($ (# chk1). attr ('checked') = undefined) // you can check whether a check has been performed.
Radio Group radio: $ (input [type = radio]). attr (checked, '2'); // set the project with value = 2 as the selected item
Drop-down box select: $ (# sel). attr (value, '-sel3'); // set the value =-sel3 project to the currently selected item
$ (

1111 2222). AppendTo (# sel) // Add the option in the drop-down box
$ (# Sel). empty (); // clear the drop-down list

Bytes ----------------------------------------------------------------------------------------------------

// Traverse option and add or remove option
Function changeShipMethod (shipping ){
Var len = $ (select [name = ISHIPTYPE] option). length
If (shipping. value! = CA ){
$ (Select [name = ISHIPTYPE] option). each (function (){
If ($ (this). val () = 111 ){
$ (This). remove ();
}
});
} Else {
$ (

UPS Ground). AppendTo ($ (select [name = ISHIPTYPE]);
}
}

// Obtain the value of the drop-down list

$ (# TestSelect option: selected'). text ();
Or $ (# testSelect). find ('option: selected'). text ();
Or $ (# testSelect). val ();
//////////////////////////////////////// //////////////////////////
You can add the following to your favorites:
1. drop-down box:

Var C0 = $ (. formc select [name = 'country'] option [selected]). text (); // get the text of the selected item from the drop-down menu (note that there is a space in the middle)
Var cc2 = $ ('. formc select [name = country]'). val (); // obtain the value of the selected item from the drop-down menu.
Var cc3 = $ ('. formc select [name = country]'). attr (id); // obtain the ID attribute value of the selected item in the drop-down menu.
$ (# Select). empty (); // clear the drop-down box // optional ({select}.html ('');
$ (

1111). AppendTo (# select) // Add the option in the drop-down box

A little explanation:
1. select [name = 'country'] option [selected] indicates that the name attribute exists,
And the select element with the selected attribute in the 'country' value;

2, single region:
$ (Input [@ type = radio] [@ checked]). val (); // obtain the value of the selected item of the single Queue (note that there is no space in the middle)
$ (Input [@ type = radio] [@ value = 2]). attr (checked, 'checked'); // set single vertex value = 2 to the selected status. (Note that there is no space in the middle)

3. Check box:
$ (Input [@ type = checkbox] [@ checked]). val (); // obtain the value of the first check box.
$ (Input [@ type = checkbox] [@ checked]). each (function () {// Since multiple check boxes are selected, You can output them cyclically.
Alert ($ (this). val ());
});

$ (# Chk1). attr (checked, ''); // do not check
$ (# Chk2). attr (checked, true); // check
If ($ (# chk1). attr ('checked') = undefined) {}// you can check whether a check has been made.

Of course, jquery's selector is powerful. There are many other methods.

<Script src = jquery-1.2.1.js type = text/javascript> </script>
<Script language = javascript type = text/javascript>
$ (Document). ready (function (){
$ (# SelectTest). change (function ()
{
// Alert (Hello );
// Alert ($ (# selectTest). attr (name ));
// Optional (a).attr(href,xx.html );
// Configure registry.location.href=xx.html;
// Alert ($ (# selectTest). val ());
Alert ($ (# selectTest option [@ selected]). text ());
$ (# SelectTest). attr (value, 2 );

});
});
</Script>

Aaass


112233445566
Jquery radio value, checkbox value, select value, radio selected, checkbox selected, select selected, and related to obtain the value of a set of radio selected items
Var item = $ ('input [@ name = items] [@ checked] '). val ();
Obtains the text of the selected select item.
Var item = $ (select [@ name = items] option [@ selected]). text ();
The second element in the select drop-down box is the selected value.
$ ('# Select_id') [0]. selectedIndex = 1;
The second element of the radio Group is the currently selected value.
$ ('Input [@ name = items] '). get (1). checked = true;
Get value:
Text Box, text area: $ (# txt). attr (value );
Multiple selection box checkbox: $ (# checkbox_id). attr (value );
Radio Group radio: $ (input [@ type = radio] [@ checked]). val ();
Drop-down box select: $ ('# sel'). val ();
Control form elements:
Text Box, text area: $ (# txt). attr (value, ''); // clear the content
$ (# Txt). attr (value, '11'); // fill in the content
Multiple choice box checkbox: $ (# chk1). attr (checked, ''); // do not check
$ (# Chk2). attr (checked, true); // check
If ($ (# chk1). attr ('checked') = undefined) // you can check whether a check has been performed.
Radio Group radio: $ (input [@ type = radio]). attr (checked, '2'); // set the project with value = 2 as the selected item
Drop-down box select: $ (# sel). attr (value, '-sel3'); // set the value =-sel3 project to the currently selected item
$ ( 2222 $ (# sel). empty (); // clear the drop-down list

Obtains the value of a set of radio selected items.
Var item = $ ('input [@ name = items] [@ checked] '). val ();
Obtain the text of the selected select item.
Var item = $ (select [@ name = items] option [@ selected]). text ();
The second element in the select drop-down box is the pre-selected value.
$ ('# Select_id') [0]. selectedIndex = 1;
The second element of the radio Group is the currently selected value.
$ ('Input [@ name = items] '). get (1). checked = true;
Get value:
Text Box, text area: $ (# txt). attr (value );
Multiple selection box checkbox: $ (# checkbox_id). attr (value );
Radio Group radio: $ (input [@ type = radio] [@ checked]). val ();
Drop-down box select: $ ('# sel'). val ();
Control form elements:
Text Box, text area: $ (# txt). attr (value, ''); // clear the content
$ (# Txt). attr (value, '11'); // fill in the content
Multiple choice box checkbox: $ (# chk1). attr (checked, ''); // do not check
$ (# Chk2). attr (checked, true); // check
If ($ (# chk1). attr ('checked') = undefined) // you can check whether a check has been performed.
Radio Group radio: $ (input [@ type = radio]). attr (checked, '2'); // set the project with value = 2 as the selected item
Drop-down box select: $ (# sel). attr (value, '-sel3'); // set the value =-sel3 project to the currently selected item
$ (

1111 2222). AppendTo (# sel) // Add the option in the drop-down box
$ (# Sel). empty (); // clear the drop-down list

Related Article

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.