This article describes in detail how to get the CheckBox and Radio values of form elements through jquery. For more information, see jquery notes:
The Code is as follows:
$ ("Input [name = 'Radio _ name']: checked"). val ()
1
2
3
The content on the Internet is too messy, and different jQuery versions may be written differently. After searching and experimenting, the content below is written under jQuery 1.3.2.
Jquery radio, checkbox, select, radio, checkbox, select, and related settings:
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 ();
Obtain the text of the selected select item: var item = $ ("select [name = items] option [selected]"). text (); or
$ ("Select [name = items]"). find ("option: selected"). text ();
The second element in the select drop-down box is the selected value: $ ('# select_id') [0]. selectedIndex = 1;
The select drop-down box value = 'val' element is the current selected item: $ ("select [name = items] option [value = 'val']"). attr ("selected", "selected ");
The second element of the radio group is currently selected: $ ('input [@ name = items] '). get (1 ). checked = true; or $ ('input [name = items] '). attr ("checked", 1 ′);
The value = 'val' element of radio is currently selected: $ ('input [name = items] [value = 'val'] '). attr ("checked", "checked ");
Get value:
Text Box, text area: $ ("# txt"). attr ("value ");
Multiple choice box checkbox: $ ("input [name = 'checkbox': checked]"). each (function (){
Var val = $ (this). val ();
});
Radio Group radio: $ ("input [type = radio] [checked]"). val ();
Value of select in the drop-down box: $ ('select'). val ();
Select the selected text value in the drop-down box: $ ("select"). find ("option: selected"). text ();
Control form elements:
Text Box, text area: $ ("# txt"). attr ("value", "); // clear the content
$ ("# Txt"). attr ("value", '11'); // fill in the content
Multiple selection box checkbox:
The second element of the checkbox is checked: $ ("input [name = items]"). get (1). checked = true; // check
$ ("Input [name = items]"). get (1). checked = false; // do not check
Check the value = 'val 'element of the checkbox: $ ("input [name = item] [value = 'val']"). attr ("checked", true); or $ ("input [name = item] [value = 'val']"). attr ("checked", "checked ");
If ($ ("input [name = item] [value = 'val']"). attr ('checked') = true) // you can check whether a check is performed.
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
$ (" 2222"). AppendTo (" # sel ") // Add the option in the drop-down box
$ ("# Sel"). empty (); // clear the drop-down list
JQuery obtains the Value selected by Radio.
Code $ ("input [name = 'Radio _ name'] [checked]"). val (); // select the Value of the selected radio
$ ("# Text_id"). focus (function () {// code...}); // event triggered when the object text_id gets the focus
$ ("# Text_id"). blur (function () {// code...}); // triggered when the focus of the object text_id is lost
$ ("# Text_id"). select (); // set the Vlaue value of the text box to the selected status.
$ ("Input [name = 'Radio _ name'] [value = 'value to select the Value of radio '").
Attr ("checked", true); // set Radio to the selected status based on the Value.
JQuery obtains the Value selected by the CheckBox.
$ ("Input [name = 'checkbox _ name'] [checked]"); // select the set of checked checkbox elements. If you want to get the Value, you need to traverse the set.
$ ("Input [name = 'checkbox _ name'] [checked]").
Each (function () {arrChk + = this. value + ',' ;}); // traverses the set of checked CheckBox elements to obtain the Value.
$ ("# Checkbox_id"). attr ("checked"); // gets the status of a CheckBox (true/false if any CheckBox is selected)
$ ("# Checkbox_id"). attr ("checked", true); // set the status of a CheckBox to select (checked = true)
$ ("# Checkbox_id"). attr ("checked", false); // set the status of a CheckBox to unselected (checked = false)
$ ("Input [name = 'checkbox _ name']"). attr
("Checked", $ ("# checkbox_id"). attr ("checked"); // you can analyze the meaning of the code based on 3, 4, and 5.
$ ("# Text_id"). val (). split (","); // returns an array separated ','.
The above operations are actually the use of the jQuery selector. I hope you will have a solid understanding of the jQuery selector.