Java code
Jquery returns the value of the radio button.
$ ("Input [name = 'items ']: checked"). val ();
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 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
I was just getting started with jquery and was unfamiliar with many things.
When $ ("# id") is used to obtain the page input element, it is found that $ ("# id"). value cannot get the value.
Finally, with the help of the great Baidu, I found the cause of the problem:
$ ("") Is a jquery object, not a dom element
Value is the attribute of dom element.
Jquery corresponds to val.
Val (): obtains the current value of the First Matching Element.
Val (val): sets the value of each matching element.
Therefore, the code should be written as follows:
Value: val = $ ("# id") [0]. value;
Assignment:
$ ("# Id") [0]. value = "new value ";
Or $ ("# id"). val ("new value ");
Alternatively, you can: val = $ ("# id"). attr ("value ");
Author: "look up at the stars"