Code for value assignment of RadioButton, input, and CheckBox in jQuery

Source: Internet
Author: User
This article mainly introduces the implementation code of RadioButton, input, and CheckBox value assignment in jQuery. if you need it, you can refer to it and hope to help you with CheckBox.

1. jquery obtains the single-choice group radio
$ ("Input [name = 'name']: checked"). val ();

2. jquery obtains the next value of radiobutton.
$ ("Input [name = 'name']: checked"). next (). text ()
$ ("Input [name = 'name']: checked"). val ()

3. jquery obtains the input value.
$ ('# ID'). val ()

4. jquery checks the multiple choice box checkbox
$ ("# Id: checkbox"). attr ("checked ")
Value: $ ("# id"). attr ("value ");
Assign values directly to values in text () and val ().

How does JQUERY obtain the text, areatext, radio, checkbox, and select values?
$ ("Input"). val ();
$ ("Textarea"). text ();
$ ("Select"). val ();

Control form elements:
Text box, text area: $ ("# txt"). attr ("value", ''); // clear the content
$ ("# Txt"). attr ("value", '11'); // fill in the content
Multiple choice box checkbox: $ ("# chk1"). attr ("checked", ''); // do not check
$ ("# Chk2"). attr ("checked", true); // Check
If ($ ("# chk1"). attr ('checked') = undefined) // You can check whether the checked has been checked.

Radio Group radio: $ ("input [@ type = radio]"). attr ("checked", '2'); // set the project with value = 2 as the currently selected item

Drop-down box select: $ ("# sel"). attr ("value", '-sel3'); // set the value =-sel3 project to the currently selected item
$ ("11112222"). AppendTo (" # sel ") // add the option in the drop-down box
$ ("# Sel"). empty (); // clear the drop-down list

JQuery obtains and sets the value of the select drop-down box. document Category:. net programming

Obtain Select:
Obtain the selected text in the select statement:
$ ("# DdlRegType"). find ("option: selected"). text ();

Obtain the value selected by the select statement:
$ ("# DdlRegType"). val ();

Obtain the index selected by the select statement:
$ ("# DdlRegType"). get (0). selectedIndex;

Set select:
Set the index selected by select:
$ ("# DdlRegType"). get (0). selectedIndex = index; // index indicates the index value.

Set the value selected by select:
$ ("# DdlRegType"). attr ("value", "Normal");
$ ("# DdlRegType"). val ("Normal ");
$ ("# DdlRegType"). get (0). value = value;

Set select selected text:
Var count = $ ("# ddlRegType option"). length;
For (var I = 0; I {If ($ ("# ddlRegType"). get (0). options [I]. text = text)
{
$ ("# DdlRegType"). get (0). options [I]. selected = true;
Break;
}
}
$ ("# Select_id option [text = 'jquery ']"). attr ("selected", true );

Set select option items:
$ ("# Select_id"). append ("Text"); // Add an option
$ ("# Select_id"). prepend ("Select"); // Insert an option
$ ("# Select_id option: last"). remove (); // delete the Option with the largest index value
$ ("# Select_id option [index = '0']"). remove (); // delete an Option with an index value of 0
$ ("# Select_id option [value = '3']"). remove (); // delete an Option with a value of 3
$ ("# Select_id option [text = '4']"). remove (); // delete an Option with a TEXT value of 4

Clear Select:
$ ("# DdlRegType"). empty ();

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.