`

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关键字用法。分享给大家供大家参考。具体分析如下: checked和unchecked是两个不常用的关键字,但是确是非常有用的关键字,对此,建议测试时开启全局checked编译器选项。 1....

    queryElectronicPolicy.uncheck

    queryElectronicPolicy.uncheck

    easyui级联树

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

    uncheck:不再处理异常

    在Java编程语言中,"uncheck:不再处理异常"这个标题可能是指开发者在代码中遇到的一个常见问题,即如何处理检查异常(checked exception)。检查异常是那些在编译时必须被处理或者声明的异常,例如IOException、...

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

    ### 关于@SuppressWarnings("unchecked")详解 在Java编程中,`@SuppressWarnings` 是一个非常有用的注解,它允许开发者暂时忽略某些编译警告。这在处理一些已知但并不影响程序运行安全性的警告时非常有用。...

    jquery checktree

    3. **事件驱动**:提供了丰富的事件接口,如`check`、`uncheck`和`toggle`,使得开发者可以监听并响应节点状态的变化,从而实现更复杂的功能。 4. **API控制**:通过API方法,可以对树的状态进行动态调整,例如`...

    queryElectronicPolicy(1).uncheck

    queryElectronicPolicy(1).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. ...

    jquery、js操作checkbox全选反选.docx

    var checkboxes = document.getElementsByName("TheID"); for (var i = 0; i &lt; checkboxes.length; i++) { checkboxes[i].checked = true; } } ``` - **解析**:使用`document.getElementsByName("TheID")`获取...

    layui动态禁用checkbox.rar

    总结来说,layui table的复选框功能可以通过列定义中的`checkbox`属性开启,通过监听`checkbox`事件和使用`checkAll`、`uncheckAll`接口进行动态控制。在处理全选时,需要注意排除已被禁用的行,以确保用户体验的...

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

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

    Uncheck the button-crx插件

    "Uncheck the button-crx插件"是一款专为西班牙语用户设计的浏览器扩展程序,它主要功能是允许用户在表单填写过程中取消选择任何单选按钮。在网页表单中,单选按钮通常用于提供一组预设选项让用户进行单一选择,而这...

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

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

Global site tag (gtag.js) - Google Analytics