Practical jquery operation Form element code

Source: Internet
Author: User

Practical jquery operation Form element code
/*

Suppose there is a button in a form id= "save"
$ (document). Ready (function () {
$ ("#save"). Click (function () {
$ ("#save"). attr ("Disabled", true);//set to not available
$ ("#form1") [0].submit ();//If you have many Form1 forms, it doesn't matter, only the first one will submit haha.
});
});


Remove the text of the selected item from the Pull menu;
Gets and sets the value of the Drop-down menu;
Empty the Drop-down menu;
Add elements to the following menu;
Take a single marquee value;
Selection of radio or check buttons;
Take the check box value;
Determines whether a single selection or check box is selected;
element is not available;
The decision element is not available.

1. Remove the text of the selected item from the Pull menu


$ ("#select option[selected]"). Text ();//select and option have spaces, option is child element of select
$ ("#select option:selected"). Text ()//if written as $ ("#select"). Text () Select all Drop-down menu Texts


2. Get and set the value of the Drop-down menu

$ ("#select"). Val ();//Fetch value
$ ("#select"). Val ("value");/setting, if there is an option in the select that has value, the option is selected, and if it does not exist, the select does not make any changes


3. Empty the Pull-down menu


$ ("#select"). empty ();
$ ("#select"). HTML ("");


4. Add elements to the following menu

$ (' <option value= "1" >1</option> "). Appendto ($ (" #select "));
$ ("#select"). Append (' <option value= ' 1 ">1</option> ');


5. Take a single marquee value

$ ("#id [checked]"). Val ();


6. Choice of radio or check button

$ ("#id [Value=val]"). attr ("Checked", true);/select
$ ("#id [Value=val]"). attr ("Checked", "");/Cancel Selection
$ ("#id [Value=val]"). attr ("checked", false);/Cancel Selection
$ ("#id [Value=val]"). Removeattr ("checked");/Cancel Selection


7. Take check box value

$ ("input[type=checkbox][checked]"). each (function () {
Alert ($ (this). Val ());
})  


If you use $ ("input[type=checkbox][checked]"). Val (), only the first selected value is returned


8. Determine if a single selection or check box is selected


if ($ ("#id"). attr ("checked")) {}//judgment selected
if ($ ("#id"). attr ("checked") ==true) {}//judgment selected
if ($ ("#id"). attr ("checked") ==undefined) {}//judgment not selected


9. Elements available are not available


$ ("#id"). attr ("disabled", false);//set to Available
$ ("#id"). attr ("Disabled", true);//set to not available


10. Determine that elements are not available


if ($ ("#id"). attr ("Disabled")) {}//judgment not available
if ($ ("#id"). attr ("disabled") ==undefined) {}//judgment available

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.