页面当中常常要作一些checkbox,radio,select,input等表单元素的操做。作为我这样的懒人早就该总结如下,免去用到的时候,不想从头写,又不得不去之前的代码中翻。下面的代码来自实际项目中,亲测可用。呵呵……
ide
1、checkboxthis
对checkbox的判断每每是checkbox有没有被勾选。也常常有一个复选框控制其它所有复选框的选中和取消选中。以下图:spa
页面左上角的复选框的选中控制表格中全部的复选框。代码以下:orm
为了操做方便,给控制全选的复选框增长一个id叫checkAllblog
下面的代码实现了全选和反选的效果get
<th><input type="checkbox" id="checkAll"/></th> input
$('#checkAll').change(function(){ if($(this).is(':checked')){ $("input[type='checkbox']").prop("checked", true); }else{ $("input[type='checkbox']").prop("checked", false); } });
获取全部选中的checkbox的值it
以上图为例,我要获取表格中每一行记录的id值,则给每个checkbox增长一个value属性,在渲染页面时将value值写到checkbox中便可。另外为了方便选取checkbox为全部的checkbox增长了一个chkbox的class。io
<td><input type="checkbox" value="` record`.`id `" class="chkbox"/></td>function
function checkValue(){ var arr = '' $(".chkbox").each(function(){ if($(this).is(":checked")){ arr += ($(this).val() + ',') } }) return arr }
未完待续……