---
title: "jquery对于checkbox一些处理"
author: "夜鸣猪"
date: "2011-06-09T10:27:34Z"
tags: []
categories: ["Web前端"]
slug: "jquery-checkbox"
draft: false
lastmod: "2011-06-18T03:41:43Z"
id: 1073950
url: "https://hlee.iteye.com/blog/1073950"
---

```html
<input type="checkbox" name="foo" value="bar" />
<input type="button" onclick="show_checked()" value="Show" />
<input type="button" onclick="set_checked(true)" value="On" />
<input type="button" onclick="set_checked(false)" value="Off" />

```



```ruby

#判断是否选中
	$('input[name=foo]').is(':checked')
	$('input[name=foo]').attr('checked')

```


```ruby

#选中和不选中
	$('input[name=foo]').attr('checked', true);
	$('input[name=foo]').attr('checked', false);

```




判断是否选中，开始用的办法




```jquery

$(function() {
  $('#isOther').click(function() {
    var other = $('#isOther:checked').val();
    if (other != undefined) $('#other').removeAttr('readonly');
    else $('#other').attr('readonly', 'readonly');
  });
});


```


其实可以


```jquery
var checked = $('input[type=checkbox]').attr('checked');
```


返回一个jason对象，是有结构的

```javascript

var myJSONObject = {"bindings": [
        {"ircevent": "privmsg", "method": "newuri", "regex": "^http://.*"},
        {"ircevent": "privmsg", "method": "deleteuri", "regex": "^delete.*"},
        {"ircevent": "privmsg", "method": "randomuri", "regex": "^random.*"}
    ]
};

myData = JSON.parse(text, function (key, value) {
    var type;
    if (value && typeof value === 'object') {
        type = value.type;
        if (typeof type === 'string' && typeof window[type] === 'function') {
            return new (window[type])(value);
        }
    }
    return value;
});


```


一些参考：
都是把一组复选框是否选中情况，得到。等着ajax发出去


```js
var sList = "";
$('input[type=checkbox]').each(function () {
    var sThisVal = (this.checked ? "1" : "0");
    sList += (sList=="" ? sThisVal : "," + sThisVal);
});
console.log (sList);
```

另一个


```js
var str = "";

$(':checkbox').each(function() {
    str += this.checked ? "1," : "0,";
});

str = str.substr(0, str.length - 1);    //Remove the trailing comma
```