JQUERY code for getting form values

Source: Internet
Author: User

Suppose we have the following page:
<Input type = "text" name = "textname" id = "text_id" value = "">
No more...

The following describes how to obtain various values in FORM;
Function get_form_value (){
/* Obtain the value of TEXT. AREATEXT */
Var textval = $ ("# text_id"). attr ("value"); // or
Var textval = $ ("# text_id"). val ();
/* Obtain the value of the single-choice button */
Var valradio = $ ("input [@ type = radio] [@ checked]"). val ();
/* Obtain the value of the check box */
Var checkboxval = $ ("# checkbox_id"). attr ("value ");
/* Get all values in the drop-down list */
Var selectval = $ ('# select_id'). val ();
// Obtain the value selected from the drop-down list. This method works for all drop-down lists.
// This method works for all the drop-down boxes
// If you obtain an ID, $ ('# id> option'). each ().

$ ('Select> option'). each (function (){
If ($ (this). attr ('selected') = true)
{
Alert ($ (this). text ());
}
})
}
3. Other processing of the form:
// Control form elements:
// Text box, text area:
$ ("# Text_id"). attr ("value", ''); // clear the content
$ ("# Text_id"). attr ("value", 'test'); // fill in the content
// Multiple choice box checkbox:
$ ("# Chk_id"). attr ("checked", ''); // value not selected
$ ("# Chk_id"). attr ("checked", true); // The selected value
If ($ ("# chk_id"). attr ('checked') = undefined) // you can check whether the selected
// Single-choice group radio:
$ ("Input [@ type = radio]"). attr ("checked", '10'); // set the single-choice button with value = 10 to the selected item
// Select from the drop-down list:
$ ("# Select_id"). attr ("value", 'test'); // set the project with value = test as the selected item
$ ("<Option value = 'test'> test </option> <option value = 'test2'> test2 </option> "). appendTo ("# select_id") // Add the option in the drop-down box
$ ("# Select_id"). empty (); // clear the drop-down list

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.