Val (), text (), and HTML () in jquery are the encapsulation of the values, innertext, and innerhtml attributes in javascrip, in jquery, if there are parameters for these functions, they are value assignment operations. If there is no parameter, they are value operations. Val () is a very important method, the form objects related to it, such as the input tag, select, and textarea, are all tag elements used for interaction with the server. Therefore, we need to clarify this Val ();
Assign values to radio, checkbox, and select:
$ ("Input [name = A]"). val (["Entertainment 1"]); $ ("input [type = checkbox]"). val (["basketball", "game"]); $ ("select "). val (["basketball", "game"]);
CodeAnalysis:
- Assign a value to radio, and the attribute selector obtains radio;
- Assign a value to the checkbox. The attribute selector obtains the checkbox. The value is enclosed in brackets []. If there are multiple values, separate them with commas;
- Assign values to select, and obtain select using the tag selector,
Operation on the value of radio, checkbox, and select:
VaR checkvalue = ""; var S = $ ("input [name = A]: checked "). val (); $ (": checkbox: checked "). each (function () {checkvalue + = $ (this ). val () ;}); var selectvalue = ""; $ ("select: Selected "). each (function () {selectvalue + = $ (this ). val () ;}); alert ("checkvalue:" + checkvalue + "radiovalue:" + S + "selectvalue:" + selectvalue );
Code Analysis:
Declare a variable of S to receive the elected value of radio of name = A. You can also write var S = $ (": Radio: checked "). val (),: radio can be understood as the input element of type = radio, which is a simplified method of attribute selector;
Obtain the selected value of the checkbox. Because the checkbox is a multiple-choice box, you must use each to process each selected item. You can also write the following content based on the attribute selector: $ ("input [type = checkbox]: checked ");
When the select attribute is multiple = "multiple", you can select multiple options. Here, we also use each for traversal;
Summary:
- For the tag elements of the input system, you can use the attribute selector to get: $ ("input [type = checkbox]"), or you can use the simple method: $ (": checkbox "), similar:
: Radio,: Submit,: image,: reset,: button,: file,: hidden,: Password,: text;
$ (": Input") select all <input> <textarea> <SELECT> and <button> Elements
- For radio and checkbox, use: checked to obtain the selected items, while for select, use: Selected ($ ("select: Selected"), $ (": Radio: checked "). val ();)
The complete example code is as follows:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> The following are the operations of JavaScript on select and checkbox:
1. checkbox operations
2. Select Operation