Check & Uncheck checkboxes

google=>js  select all

http://www.webdevdoor.com/javascript-ajax/javascript-function-check-uncheck-checkboxes/

 

Check & Uncheck checkboxes in JavaScript

Here’s a useful Javascript function I recently coded to allow a list of checkboxes in a form to be selected or unselected. The CheckAll function takes in two parameters, the first is the form name and the second is a boolean value – true or false, true being to select all checkboxes and false to un-select all.

1
2
3
4
5
6
7
8
9
10
11
12
13
<script type= "text/javascript" language= "javascript" > // <![CDATA[
function checkAll(formname, checktoggle)
{
   var checkboxes = new Array(); 
   checkboxes = document[formname].getElementsByTagName( 'input' );
  
   for ( var i=0; i<checkboxes.length; i++)  {
     if (checkboxes[i].type == 'checkbox' )   {
       checkboxes[i].checked = checktoggle;
     }
   }
}
// ]]></script>

The function gets the array of checkboxes from the selected form from their HTML tag ‘input’ type. If there are other input types such as ‘radio’, the function loops through all the input fields in the form, selecting only those that are checkboxes.

To call the function, add the following javascript command to your check all and uncheck all text or image links:

1
2
< a onclick = "javascript:checkAll('form3', true);" href = "javascript:void();" >check all</ a >
< a onclick = "javascript:checkAll('form3', false);" href = "javascript:void();" >uncheck all</ a >

One of the advantages of using this method is that unlike some other similar functions, this check all works regardless of the name of the checkbox. There may be occasions for example where the checkbox name changes within a list, i.e. ‘checkbox1′, ‘checkbox2′ etc rather than ‘checkbox’ for the whole list.

The Javascript check all function has been tested for compatibility in IE8, IE9, Chrome & FireFox

JQuery Check & Uncheck checkboxes

If you prefer a jQuery check/uncheck function, the below code will achieve exactly the same as above. This code will target all checkboxes on a page using the $(“input:checkbox”) selector.

1
2
3
4
5
6
7
8
$(document).ready( function () {
   $( '#check-all' ).click( function (){
     $( "input:checkbox" ).attr( 'checked' , true );
   });
   $( '#uncheck-all' ).click( function (){
     $( "input:checkbox" ).attr( 'checked' , false );
   });
});

Rather than adding the check all function to the link itself, the jQuery version listens for when the ‘check all’ or ‘uncheck all’ link is clicked, which means these links will need an ID adding to them as below:

1
2
<a id= "check-all" href= "javascript:void(0);" >check all</a>
<a id= "uncheck-all" href= "javascript:void(0);" >uncheck all</a>

If you wanted to target specific checkboxes instead of all checkboxes on the page, you could add a class to the input which allows you to check/uncheck the boxes by replacing the code $(“input:checkbox”).attr(‘checked’, true); with $(“.checkboxclass”).attr(‘checked’, true);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值