jquery operation Select, checkbox, Radio detailed explanation

Source: Internet
Author: User

First, Select

jquery Gets the text and value selected by select:

1. $ ("#select_id"). Change (function () {//code ...}); Add an event for Select, which is triggered when one of the items is selected

2. Var checktext=$ ("#select_id"). Find ("option:selected"). Text (); Gets the text selected by the Select

3. Var checkvalue=$ ("#select_id"). Val (); Gets the value selected by select

4. Var checkindex=$ ("#select_id"). Get (0). SelectedIndex; Gets the index value of the Select selection

5. Var maxindex=$ ("#select_id option:last"). attr ("index"); Gets the maximum index value of the Select

jquery sets the text and value selected for select:

1. $ ("#select_id"). Get (0). selectedindex=1; Set the Select index value to 1 to select

2. $ ("#select_id"). Val (4); Sets the value of select to 4 for the item selected

3. $ ("#select_id option[text= ' jQuery ']"). attr ("selected", true); Set the text value of select to the item selected for jquery

jquery Add/Remove option for select:

1. $ ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Append an option to the Select (drop-down item)

2. $ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Insert an option for select (first position)

3. $ ("#select_id option:last"). Remove (); Delete index value in select maximum option (last)

4. $ ("#select_id option[index= ' 0 ']"). Remove (); Delete option with index value 0 in select (first)

5. $ ("#select_id option[value= ' 3 ']"). Remove (); Delete option for value= ' 3 ' in select

6. $ ("#select_id option[text= ' 4 ']"). Remove (); Delete option for text= ' 4 ' in select

7. $ ("#SelectID"). Remove (); Delete all items

Second, Checkbox

Select All/Cancel

Jquery.attr Gets/Sets the property value of the object, such as:

$ ("Input[name= ' chk_list ')"). attr ("checked"); Reads the status of all name ' Chk_list ' objects (checked)

$ ("Input[name= ' chk_list ')"). attr ("Checked", true); Set all checked of name ' Chk_list ' to True

$ ("#img_1"). attr ("src", "test.jpg"); Set the value of SRC with ID img_1 to ' test.jpg '

$ ("#img_1"). attr ("src"); Read SRC value with ID img_1

The following code is a value that gets 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 value of the checkbox

$ ("#checkboxID"). Value or $ ("input[type= ' checkbox ']"). EQ (n). attr ("Checked"). Value

2, set the selected item

$ ("input[type= ' checkbox ']"). EQ (1). attr ("checked")//Set the first checkbox to the selected item

3, delete all checkboxes

$ ("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"));

}

})

});

});

Third, radio

1, gets the value selected

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

2, sets the specified item as the currently selected item

$ ("input[type= ' Radio ')"). EQ (1). attr ("Checked", true);//Set the second item to be selected

$ ("input[type= ' Radio '][value= ' value ']"). attr ("checked, true");

3, solve multiple radio

$ ("input[type= ' Radio ' [@name = ' rdoTest2 ']"). EQ (0). attr ("Checked", true);

jquery operation Select, checkbox, Radio detailed explanation

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.