---
title: "反选 并修改选中的数量"
author: "bask"
date: "2008-07-03T14:13:09Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2008-07-03T14:13:09Z"
id: 211044
url: "https://bask.iteye.com/blog/211044"
---

```js

<script>  
	var j=0//全局使用  
	jQuery(function ($) {  
		//加载触发  
		$("input[@type='checkbox']").each(function() {  
			if($(this).attr("checked")) {  
				j++;  
			} else {  
				j--;  
			}  
			$("#count").html(""+j);  
		});  
		$("#count").html(""+j);  
		//反选  
		$("#otherCheck").click(function(){  
			j = 0;  
			$("input[@type='checkbox']").each(function() {  
				if($(this).attr("checked")) {  
					$(this).attr("checked",false);  
				} else {  
					$(this).attr("checked",true);  
					j++;  
				}  
			});  
			$("#count").html(""+j);  
		});  
	});  
</script>

```


```html

<span id="count"></span>
<input type="checkbox" />
<input type="button" id="otherCheck" value="反选" />

```

如果只是反选

```js

 $("input[@type='checkbox']").each(function() {
    $(this).attr("checked",!$(this).attr("checked"));
  });

```