Jquery obtains the selected text and value of select.

Source: Internet
Author: User
Jquery obtains the text and value selected by the SELECT statement (jquery learning Memorandum)

 

Syntax explanation:
1. $ ("# select_id"). Change (function () {// code...}); // Add an event for select, triggered when one of the select events is selected
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 largest 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 jquery.

Jquery adds/deletes select option items:

Syntax explanation:

1. $ ("# select_id"). append ("<option value = 'value'> text </option>"); // append an option for select (drop-down)
2. $ ("# select_id "). prepend ("<option value = '0'> select </option>"); // insert an option for select (first position)
3. $ ("# select_id option: Last"). Remove (); // Delete the largest option (last) index value in select)
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 the SELECT statement
5. $ ("# select_id option [TEXT = '4']"). Remove (); // Delete the option of '4' text = '4' in select

 

 

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 ();
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 selected value.
$ ('# Select_id') [0]. selectedindex = 1;
The second element of the radio Radio Group is the currently selected value.
$ ('Input [@ name = items] '). Get (1). Checked = true;

Get value:

Text Box, text area: $ ("# TXT"). ATTR ("value ");
Multiple choice 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 the checked has been checked.

Radio Group radio: $ ("input [@ type = radio]"). ATTR ("checked", '2'); // set the project with value = 2 as the currently selected item
Drop-down box select: $ ("# Sel"). ATTR ("value", '-sel3'); // set the value =-sel3 project to the currently selected item
$ ("<Option value = '1'> 1111 </option> <option value = '2'> 2222 </option> "). 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 {
$ ("<Option value = '000000'> ups ground </option>"). 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 ();
//////////////////////////////////////// //////////////////////////
If your memory is poor, you can add it to your favorites:
1. drop-down box:

VaR PC3= $ (". 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 list.
VaR CC3 = $ ('. formc select [@ name = "country"]'). ATTR ("ID"); // obtain the ID attribute value of the selected item from the drop-down menu
$ ("# Select"). Empty (); // clear the drop-down box // $ ("# select" example .html ('');
$ ("<Option value = '1'> 1111 </option>"). 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,
In addition, the Select element with the selected attribute in the 'country' select element has the selected attribute;
It can be seen that the attribute is followed by the @ parameter.

2, single region:
$ ("Input [@ type = radio] [@ checked]"). Val ();// Obtain the value of the selected option for a single Queue (note that there is no space in the middle)
$ ("Input [@ type = radio] [@ value = 2]"). ATTR ("checked", 'checked'); // set single checked 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 performed.


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 "));
// $ ("A"). ATTR ("href", "xx.html ");
// Window. Location. href = "xx.html ";
// Alert ($ ("# selecttest"). Val ());
Alert ($ ("# selecttest option [@ selected]"). Text ());
$ ("# Selecttest"). ATTR ("value", "2 ");

});
});
</SCRIPT>


<A href = "#"> aaass </a>

<! -- Drop-down box -->
<Select id = "selecttest" name = "selecttest">
<Option value = "1"> 11 </option>
<Option value = "2"> 22 </option>
<Option value = "3"> 33 </option>
<Option value = "4"> 44 </option>
<Option value = "5"> 55 </option>
<Option value = "6"> 66 </option>
</SELECT>
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 ();
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 selected value.
$ ('# Select_id') [0]. selectedindex = 1;
The second element of the radio Radio Group is the currently selected value.
$ ('Input [@ name = items] '). Get (1). Checked = true;
Get value:
Text Box, text area: $ ("# TXT"). ATTR ("value ");
Multiple choice 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 the checked has been checked.
Radio Group radio: $ ("input [@ type = radio]"). ATTR ("checked", '2'); // set the project with value = 2 as the currently selected item
Drop-down box select: $ ("# Sel"). ATTR ("value", '-sel3'); // set the value =-sel3 project to the currently selected item
$ ("<Optionvalue = '1'> 1111 </option> <optionvalue = '2'> 2222 </option> "). appendto ("# Sel") // Add the option in the drop-down box
$ ("# 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 selected value.
$ ('# Select_id') [0]. selectedindex = 1;
The second element of the radio Radio Group is the currently selected value.
$ ('Input [@ name = items] '). Get (1). Checked = true;
Get value:
Text Box, text area: $ ("# TXT"). ATTR ("value ");
Multiple choice 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 the checked has been checked.
Radio Group radio: $ ("input [@ type = radio]"). ATTR ("checked", '2'); // set the project with value = 2 as the currently selected item
Drop-down box select: $ ("# Sel"). ATTR ("value", '-sel3'); // set the value =-sel3 project to the currently selected item
$ ("<Option value = '1'> 1111 </option> <option value = '2'> 2222 </option> "). appendto ("# Sel") // Add the option in the drop-down box
$ ("# Sel"). Empty (); // clear the drop-down list

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.