1、默认全选
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<div class="margin"> <p>推文群发(多账号列表) 全部选中<input type="checkbox" class="check-head" /></p> <ul class="list-inline list-unstyle"> {volist name="twitterAccounts" id="ta"} <li><input type="checkbox" value="{$ta.id}" name="id[]" class="check-item" /> {$ta.name} </li> {/volist} </ul> </div> <script type="text/javascript"> $(document).ready(function(){ //全部选中 $('.check-item').prop("checked", true); }); </script> |
2、全选函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* 全选实现 * @head 全选按钮的class名 * @item 要选择的所有input的class名 * */ function checkAll(head, item) { $('.' + head).click(function () { $('.' + item).prop("checked", this.checked); }); $('.' + item).click(function () { var option = $('.' + item); option.each(function (i) { if (!this.checked) { $('.' + head).prop("checked", false); return false; } else { $('.' + head).prop("checked", true); } }); }); } |
3、判断至少选中一个复选框
1 2 3 4 |
if($('.check-item:checked').size() < 1){ alert('请先选中需发布的账号!'); return false; } |
4、读取选中复选框的值
1 2 3 4 5 6 7 8 |
var arr = []; $('.check-item').each(function(i){ if(this.checked){ arr.push(this.value); } }); var snsTwitter = arr.toString(); |
转载请注明:PHP笔记 » JavaScript操作复选框单选框的相关技巧