JS Writing of original ecology
Get the selected data value from a form checkbox:
The code is as follows |
Copy Code |
<input type= "checkbox" Name= "CB" value= "1"/>aa <input type= "checkbox" Name= "CB" value= "2"/>bb <input type= "checkbox" Name= "CB" value= "3"/>cc <input type= "checkbox" Name= "CB" value= "4"/>dd <br/> <input type= "button" value= "Submit" onclick= "checkedvalues ()"/> Using native JS writing: function Checkedvalues () { var arr=new Array (); var checkbox=document.getelementbyname (' CB '); For (var i=0;i<checkbox.length (); i++) { if (checkbox[i].checked==true) { Arr.push (Checkbox[i].value); alert (Checkbox[i].value); } } }
|
It's JS now, so let's take a look at the popular jquery
/* Get check box value * *
The code is as follows |
Copy Code |
var checkboxval = $ ("#checkbox_id"). attr ("value"); |
Cases
The code is as follows |
Copy Code |
<input type= "checkbox" Name= "Checkboxid" value= "Biuuu_c1"/>c1 <input type= "checkbox" Name= "Checkboxid" value= "BIUUU_C2"/>c2 <input type= "checkbox" Name= "Checkboxid" value= "BIUUU_C3"/>c3 <input type= "checkbox" Name= "Checkboxid" value= "BIUUU_C4"/>c4 <input type= "button" id= "Cbuttonid" value= "Get checkbox value"/> <span id= "Cresult" ></span> |
jquery Code
The code is as follows |
Copy Code |
$ ("#cButtonId"). Click (function () {//Get CheckBox Object var Check = '; var checkedobj = $ ("[name= ' Checkboxid '] [@checked]")//Gets the value value of the current checked if more than one loop is selected Checkedobj.each (function () {var ischeck = This.value; Check + = Ischeck; }); $ ("#cResult"). HTML ("Result:" +check); }); |
Examples show that mastering the object invocation of jquery is very useful for getting form elements, you can get the dropdown box selected value, the Duplicate/radio box checked value, the literal text value value, enhances our form element's interactivity and the flexibility, More information can be referred to: http://www.111cn.net/wy/jquery/47153.htm