---
title: "jquery checkbox"
author: "endual"
date: "2013-05-06T10:15:35Z"
tags: []
categories: ["Web前端"]
slug: "jquery-checkbox"
draft: false
lastmod: "2013-05-06T10:15:35Z"
id: 1860255
url: "https://endual.iteye.com/blog/1860255"
---

It certainly is in

## jQuery 1.6+

Use the new [`.prop()`](http://api.jquery.com/prop) function:

```
$('.myCheckbox').prop('checked',true);
$('.myCheckbox').prop('checked',false);
```

## jQuery 1.5 and below

The `.prop()` function is not available, so you need to use [`.attr()`](http://api.jquery.com/attr).

To check the checkbox (by setting the value of the checked attribute) do

```
$('.myCheckbox').attr('checked','checked');
```

and for un-checking (by removing the attribute entirely) do

```
$('.myCheckbox').removeAttr('checked');
```

## Any version of jQuery

If you're working with just one element, it will always be fastest to use `DOMElement.checked = true`. The benefit to using the `.prop()` and `.attr()` functions is that they will operate on all matched elements.

```
// Assuming an event handler on a checkboxif(this.checked)
```