Jquery entry 3: form object operations

Source: Internet
Author: User

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:

  1. Assign a value to radio, and the attribute selector obtains radio;
  2. 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;
  3. 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;
      1. Summary:

     

        • 22
            1. 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:
            2. : Radio,: Submit,: image,: reset,: button,: file,: hidden,: Password,: text;

              $ (": Input") select all <input> <textarea> <SELECT> and <button> Elements

            3. 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

           

           

           

           

        Related Article

        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.