Jquery operations such as select, checkbox, and radio are described in detail!

Source: Internet
Author: User
I. Select


Jquery gets the selected text and value of select:

1. $ ("# select_id"). Change (function () {// code...}); // Add an event for select, triggered when one of the select events is selected
2. var checktext = $ ("# select_id"). Find ("option: Selected"). Text (); // obtain the selected text
3. var checkvalue = $ ("# select_id"). Val (); // obtain the value selected by select
4. var checkindex = $ ("# select_id"). Get (0). selectedindex; // obtain the index value selected by select
5. var maxindex = $ ("# select_id option: Last"). ATTR ("Index"); // obtain the largest index value of select.

 

Jquery sets the text and value selected by select:

1. $ ("# select_id"). Get (0). selectedindex = 1; // select an item whose select index value is 1
2. $ ("# select_id"). Val (4); // select an item whose value is set to 4.
3. $ ("# select_id option [TEXT = 'jquery ']"). ATTR ("selected", true); // set the select text value to jquery.

 

Jquery adds/deletes select option items:

1. $ ("# select_id"). append ("<option value = 'value'> text </option>"); // append an option for select (drop-down)
2. $ ("# select_id "). prepend ("<option value = '0'> select </option>"); // insert an option for select (first position)
3. $ ("# select_id option: Last"). Remove (); // Delete the largest option (last) index value in select)
4. $ ("# select_id option [Index = '0']"). Remove (); // Delete the option with the index value 0 in select (the first option)
5. $ ("# select_id option [value = '3']"). Remove (); // Delete the option with value = '3' In the SELECT statement
6. $ ("# select_id option [TEXT = '4']"). Remove (); // Delete the option of '4' text = '4' in select

7. $ ("# selectid"). Remove (); // delete all items

Ii. checkbox

Select All/cancel


Jquery. ATTR gets/Sets object attribute values, such:
 

$ ("Input [name = 'chk _ list']"). ATTR ("checked"); // read the status of all objects whose names are 'chk _ List' (whether or not selected)
$ ("Input [name = 'chk _ list']"). ATTR ("checked", true); // set the checked value of all objects whose names are 'chk _ list' to true.
$ ("# Img_1"). ATTR ("src", "test.jpg"); // set the value of srcto 'test.jpg 'with the ID of img_1'
$ ("# Img_1"). ATTR ("src"); // read the SRC value whose ID is img_1

The following code gets the value of the checkbox selected in the above instance:
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
VaR arrchk = $ ("input [name = 'chk _ list'] [checked]");
$ (Arrchk). Each (function (){
Window. Alert (this. value );
});
});
</SCRIPT>

1. Get the checkbox Value

$ ("# Checkboxid"). value or $ ("input [type = 'checkbox']"). eq (N). ATTR ("checked"). Value

2. Set the selected items

$ ("Input [type = 'checkbox']"). eq (1). ATTR ("checked") // you can specify the first checkbox as the selected item.

3. Delete all checkpoints.

$ ("Input [type = 'checkbox']"). Remove ()

4. checkbox Method

$ (Document). Ready (function (){
VaR check = $ ("input [type = 'checkbox']");
Check. Each (function (n ){
Check. eq (N). BIND ("click", function (){
If (check. eq (N). ATTR ("checked ")! = False ){
VaR value = check. eq (N). Val ();
Alert (value );
}
Else {
Alert (check. eq (N). ATTR ("checked "));
}
})
});
});

3. radio1: Obtain the selected value.

$ ("Input [type = 'Radio ']: checked"). Val ();
 

2. Set the specified item to the selected item.

$ ("Input [type = 'Radio ']"). eq (1). ATTR ("checked", true); // set the second item to the selected item

$ ("Input [type = 'Radio '] [value = 'value']"). ATTR ("checked, true"); 3, solves multiple radio
$ ("Input [type = 'Radio '] [@ name = 'rdotest2']"). eq (0). ATTR ("checked", true );


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.