Jquery knowledge point 3 jquery form object operation_jquery

Source: Internet
Author: User
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:

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.