Operations on the input control in jQuery

Source: Internet
Author: User

Jquery radio, checkbox, select, radio, checkbox, select, and related 1. obtain the value of jquery. Obtain the value of the radio button [html] $ ("input [name = 'items ']: checked "). val (); get the value of a set of items selected by radio [html] var item =$ ('input [name = items] [checked] '). val (); obtain the text of the selected select item [html] var item =$ ("select [name = items] option [selected]"). text (); the second element in the select drop-down box is the currently selected value [html] $ ('# select_id') [0]. selectedIndex = 1; the second element of the radio Group is the currently selected value [html] $ ('input [nam E = items] '). get (1 ). checked = true; text box, text area: [html] $ ("# txt "). attr ("value"); multiple choice box checkbox: [html] $ ("# checkbox_id "). attr ("value"); single-choice radio: [html] $ ("input [type = radio] [checked]"). val (); drop-down box select: [html] $ ('# sel '). val (); 2. control Form element: text box, text area: [html] $ ("# txt "). attr ("value", ''); // clear the content $ (" # txt "). attr ("value", '11'); // multiple-choice box for filling content checkbox: [html] $ ("# chk1 "). attr ("checked", ''); // do not tick $ (" # chk2 "). attr ("checked", true );// Tick if ($ ("# chk1 "). attr ('checked') = undefined) // determines whether the radio group radio has been checked: [html] $ ("input [type = radio]"). attr ("checked", '2'); // select: $ ("# sel") for the project whose value is set to 2 "). 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 option $ ("# sel") in the drop-down box "). empty (); // clear the drop-down list. Note: When $ ("# id") is used to obtain the input element of the page, $ ("# id") is found "). the value cannot be obtained. Later, with the help of the great Baidu, the problem was found. Cause: $ ("") is a jquery object, instead of a dom element value. It is the attribute of dom element. jquery corresponds to 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: [html]; Value: val = $ ("# id") [0]. value; value: $ ("# id") [0]. value = "new value"; or $ ("# id "). val ("new value"); or you can: val = $ ("# id "). attr ("value ");

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.