Simple code for using jquery to operate form elements and jquery form elements

Source: Internet
Author: User

Simple code for using jquery to operate form elements and jquery form elements

Take the text of the selected item from the drop-down menu; Obtain and set the value of the drop-down menu; clear the drop-down menu; add elements to the following menu; obtain the single-choice value; select a single choice or check button; select the value of the check box, determine whether a single choice or check box is selected, whether the element is available or unavailable, and whether the element is available or unavailable.

Practical jquery operation form element code

/* Assume that there is a button id = "save" $ (document) in a form ). ready (function () {$ ("# save "). click (function () {$ ("# save "). attr ("disabled", true); // set to unavailable $ ("# form1") [0]. submit (); // It doesn't matter if you have many forms with the id of form1. Only the first form will be submitted Haha .});});

Take the text of the selected item from the drop-down menu;

Obtain and set the value of the drop-down menu;

Clear the drop-down menu;

Add elements to the following menu;

Returns the value of a single sequence;

Select a single or multiple-choice button;

Check box value;

Determines whether a single choice or check box is selected;

The element is available and unavailable;

Determine whether the element is available or not.

1. Take the text of the selected item from the drop-down menu

$ ("# Select option [selected]"). text (); // there is a space between select and option. option is the sub-element of select $ ("# select option: selected "). text (); // if it is written as $ ("# select "). text (); select all the text from the drop-down menu.

2. Get and set the drop-down menu Value

$ ("# Select "). val (); // value $ ("# select "). val ("value"); // set. If the select option has the value option, the option is selected. If the option does not exist, the select option is not changed.

3. Clear the drop-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. Obtain the single-digit value.

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

6. select a single choice or check button

$ ("# Id [value = val]"). attr ("checked", true); // select $ ("# id [value = val]"). attr ("checked", ""); // deselect $ ("# id [value = val]"). attr ("checked", false); // deselect $ ("# id [value = val]"). removeattr ("checked"); // cancel the selection

7. Check box Value

$ ("Input [type = checkbox] [checked]"). each (function () {alert ($ (this ). val ();}) // If $ ("input [type = checkbox] [checked]") is used. val (), only the first selected value is returned

8. Determine whether a single choice or check box is selected

If ($ ("# id "). attr ("checked") {}// determines if ($ ("# id") selected "). attr ("checked") = true) {}// determines if ($ ("# id") selected "). attr ("checked") = undefined) {} // you can specify the value of "not selected ".

9. The element is available and unavailable.

$ ("# Id "). attr ("disabled", false); // set to available $ ("# id "). attr ("disabled", true); // set to unavailable

10. Determine whether the element is available or not

If ($ ("# id "). attr ("disabled") {}// determines if ($ ("# id") is unavailable "). attr ("disabled") = undefined) {} // determines whether the service is available

The simple code for the above practical jquery operation form elements is all the content shared by the editor. I hope you can give us a reference and support the help house.

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.