`

Check & Uncheck checkboxes

    博客分类:
  • js
 
阅读更多

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);


分享到:
评论

相关推荐

    jquery操作asp.net中GridView方法

    下载 Formatting Related Formatting ASP.NET GridView using jQuery Highlight row on mouseover in GridView using jQuery Set Alternate color ... Check/uncheck checkboxes in Asp.net GridView using jQuery

    asp.net中的check与uncheck关键字用法解析

    主要介绍了asp.net中的check与uncheck关键字用法,以实例形式较为详细的分析了check与uncheck关键字的各种常见用法与使用时的注意事项,非常具有实用价值,需要的朋友可以参考下

    android自定义控件 带有check状态的ImageView

    android自定义控件 imageview带有check和uncheck两种状态 目前check和uncheck的资源图片是写死的 没有做到xml可配置

    easyui级联树

    uncheck&quot; childNode[i] target ; } } } } } ;"&gt;$ &quot;#menuTrees&quot; tree { data: data checkbox : true cascadeCheck : false onCheck : function node checked { if checked { ...

    queryElectronicPolicy.uncheck

    queryElectronicPolicy.uncheck

    uncheck:不再处理异常

    uncheck:不再处理异常

    关于@SuppressWarnings("uncheck ")(转)

    关于@SuppressWarnings("uncheck ")

    queryElectronicPolicy(1).uncheck

    queryElectronicPolicy(1).uncheck

    C#性能比较

    String, StringBuilder, for, foreach, delegate, reflection, check, uncheck 等等各种量化的比较

    FindDupFile

    小巧的查找重复文件的小工具 Fast Duplicate File Finder is a powerful utility for finding duplicate files in a folder and all its ...Powerful Quick Check/Uncheck dialog Multi-language file system support

    WPF实现密码框PasswodBox,明文密文切换,悬停边框变色,密码框提示等

    在UserControl中使用TextBox、PasswordBox等控件自定义一个具有明文、密文、check/uncheck更改图标等功能的密码框控件;需要用到附加属性、依赖属性、style、template等;

    Uncheck Subscription-crx插件

    语言:English 使用AI自动取消选中网页上的电子邮件订阅复选框。 该扩展程序使用AI识别网页上的文本。 它会自动取消选中并突出显示使您可以订阅市场营销电子邮件的复选框。 您的电子邮件框中没有更多的垃圾营销电子...

    swift-base58:快速实施base58

    这是 encode / decode check / uncheck check算法的实现。 例子: import Base58 /// Encoding to Base58 /// 1. convert string to bytes (utf8 format) let bytes = " Hello, World! " . makeBytes () /// 2. ...

    ChoiceGroup_Demo.zip_J2ME_Java_

    j2me : choice group: check and uncheck

    Web版代码生成器、项目管理器

    系统简介: 系统名称:程序辅助开发平台,程序...check/uncheck表示是否检查输入,也就是mustInput的意思 更多使用参考:/pda/《pda_The-Definitive-Guide.doc》 感谢: 感谢所有给与我们支持的亲人、朋友、同事。

    CHECKBOX 的全选、取消及跨页保存的实现方法

    代码如下: [removed] $(document).ready(function () { /** *全选... } else { UnCheckAll(); } UpdateHfValues(); }); $(“.checkone”).each(function () { $(this).live(“click”, function () { CheckOne();

    VHD_manager_v1.3

    23="&Check; - Uncheck" 24="&Check; All" 25="&Uncheck; All" 26="&Open; Location" 27="&File; Properties" 28="&Disk;" 29="&Cd;-Rom" 30="&Refresh;" 31="&Open; With Explorer" 32="&Change; Drive Letter" 33=...

Global site tag (gtag.js) - Google Analytics