Val (), text (), html () in Jquery () is to encapsulate the value, innerText, and innerHtml attributes in the javascrip in Jquery. If there is a parameter, it is a value assignment operation. If there is no parameter, it is a value operation, where val () it 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, so we need to figure out this val ();
Assign values to radio, checkbox, and select:
The Code is as follows:
$ ("Input [name = a]"). val (["Entertainment 1"]);
$ ("Input [type = checkbox]"). val (["basketball", "game"]);
$ ("Select"). val (["basketball", "game"]);
Code Analysis:
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:
The Code is as follows:
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 AndElementFor radio and checkbox, use: checked to obtain the selected items, while for select, use: selected ($ ("select: selected"), $ (": radio: checked "). val ();) The Code is as follows: