jquery Operation radio,checkbox,select form operation implementation code _jquery

Source: Internet
Author: User

One, Select
jquery Gets the text and value selected by select:
1. $ ("#select_id"). Change (function () {//code ...}); Adds an event for a select that fires when one of the items is selected
2. Var checktext=$ ("#select_id"). Find ("option:selected"). Text (); Gets the text selected by select
3. Var checkvalue=$ ("#select_id"). Val (); Gets the value of the Select selection
4. Var checkindex=$ ("#select_id"). Get (0). SelectedIndex; Get the index value selected by select
5. Var maxindex=$ ("#select_id option:last"). attr ("index"); Get the maximum index value for select

jquery Sets the Select selected text and value:
1. $ ("#select_id"). Get (0). selectedindex=1; To set the Select index value of 1
2. $ ("#select_id"). Val (4); Select to set the value of 4 for the Select
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 items for select:
1. $ ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Append an option to select (dropdown)
2. $ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Inserts an option for the Select (first position)
3. $ ("#select_id option:last"). Remove (); Delete index value maximum option (last) in select
4. $ ("#select_id option[index= ' 0 ']"). Remove (); Delete option with index value of 0 in select (first)
5. $ ("#select_id option[value= ' 3 ']"). Remove (); Delete option value= ' 3 ' in select
6. $ ("#select_id option[text= ' 4 ']"). Remove (); Delete option text= ' 4 ' in select
7. $ ("#SelectID"). Remove (); Delete all items

Second, Checkbox
Select All/Cancel
Jquery.attr Gets/Sets the property value of an 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); Sets the checked of all name ' Chk_list ' objects to True
$ ("#img_1"). attr ("src", "test.jpg"); Set the value of &LT;IMG&GT;SRC with ID img_1 as ' test.jpg '
$ ("#img_1"). attr ("src"); Read &LT;IMG&GT;SRC value with ID img_1
The following code gets the value of the checkbox selected in the previous instance:
<script type= "Text/javascript" >
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 checkbox
$ ("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, get the selected value
$ ("input[type= ' Radio ']:checked"). Val ();
2, sets the specified item to the current 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, solve multiple radio

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

After running, please refresh to see the effect, save to run locally, there is no problem.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>jquery operation radio,checkbox,select</title> <script src=" http://img.jb51.net /jslib/jquery/jquery14.js "></script> </pead> <body> <script type=" Text/javascript "> $ ( Function () {$ (' #uname '). blur (function () {if ($ (' #uname '). val () = = ') {$ (' #err '). HTML (' * '); else {$ (' #err '). html ('); } }); $ (' #see '). Click (function () {if ($ (' #uname '). val () = = ') {$ (' #err '). HTML (' * '); else {$ (' #err '). html ('); $ (' #name '). HTML ($ (' #uname '). Val ()); }//$ (' #xingbie '). HTML ($ (': radio:checked '). Val ()); $ (' #xingbie '). HTML ($ ("[name= ' AA ']:checked"). Val ()); var str= ""; $ ("[name= ' BBB ']:checked"). each (function () {str+=$ (this). Val () + ""; }); $ (' #shengfen '). html (str); $ (' #nianFen '). HTML ($ (' #nf '). Find (' option:selected '). text ());//text $ (' #nianfen '). The HTML ($ (' #nf '). Find (' option:selected '). Val ());//value}); $ (' #quan '). Click (function () {$ ("[name= ' CCC ']"). attr (' checked ', ' true '); }); $ (' #buxuan '). Click (function () {$ ("[name= ' CCC ']"). Removeattr (' checked '); }); $ (' #fan '). Click (function () {$ ("[name= ' CCC ']"). each (function () {if ($ (this). attr ("checked")) {$ (this). Removeattr (" Checked "); } else {$ (this). attr ("Checked", ' true '); } }); }); $ (' #kan '). Click (function () {var ss= ""; $ ("[name= ' CCC ']:checked"). each (function () {ss+=$ (this). Val () + ""; }); $ (' #seee '). HTML (ss); }); }); </script> <table width= "border=" 1 "cellspacing=" 1 "cellpadding=" 1 "> <tr> <td width=" 30% "Alig n= "Center" > Username:</td> <td width= "60%" ><input type= "text" id= "uname"/></td> <td "width=" 10% "><div id=" err "></div></td> </tr> <tr> <td align=" center ">   & nbsp Don't:</td>. <td><input name= "AA" type= "Radio" id= "ta1" value= "male" checked= "checked"/> male   <input name= "AA" Type= "Radio" id= "TA2" value= "women"/> Women </td> <td> </td> </tr> <tr> <td align= "cen ter "> Province    :</td> <td><input name=" bbb "type=" checkbox "id=" Ck1 "value=" Beijing "/> Beijing <input name= "BBB" type= "checkbox" id= "Ck2" value= "Shanghai"/> Shanghai </td> <td> </td> </tr> & lt;tr> <td align= "Center" > Year    :</td> <td><select id= "NF" > <option Value= "2008" >2008 years </option> <option value= "2009" >2009 years </option> <option value= "" "> 2010 </option> </select></td> <td> </td> </tr> <tr> <td align= "cen ter "> Output name </td> <td><div id=" name "></div></td> <td> </td> </tr > <tr> <td align= "center" > Output Sex </td><td><div id= "Xingbie" ></div></td> <td> </td> </tr> <tr> <td align= "Center" > Export Province </td> <td><div id= "Shengfen" ></div></td> <td> < /TD> </tr> <tr> <td align= "center" > Output Year </td> <td><div id= "Nianfen" ></div& gt;</td> <td> </td> </tr> <tr> <td align= "center" > Junction     Fruit: </td> <td><input type= "button" id= "" "value=" Output "/></td> <td> </td> < /tr> </table> <table width= "$" border= "1" cellspacing= "1" cellpadding= "1" > <tr> <td align= "Center" > Province    :</td> <td><input name= "CCC" type= "checkbox" id= "Cck1" value= "Beijing" > Beijing <input name= "CCC" type= "checkbox" id= "Cck2" value= "Shanghai"/> Shanghai <input name= "CCC" type= "checkbox" id= "Cck3" value= "Guangzhou"/> Guangzhou </td> <td> </td> </tr> <tr> <td align= "center" > </td> <td><input name= "CCC" type= "checkbox" Id= " Cck4 "value=" Tianjin/> Tianjin <input name= "CCC" type= "checkbox" id= "Cck5" value= "other"/> Other </td> <td>  </td> </tr> <tr> <td align= "center" > Lost    :</td> <td><div id= " Seee "></div></td> <td> </td> </tr> <tr> <td align=" center "> Knot     Fruit:</td> <td><input type= "button" id= "Quan" value= "Select All"/><input type= "button" Id= "Buxuan" value= "/><input type=" button "id=" Fan "value=" select "/><input type=" button "id=" Kan "value=" view "/></td> <td> </td> </tr> </table> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.