---
title: "（原）关于.attr(“checked”, true)无法修改值得问题"
author: "I_conquer"
date: "2014-11-12T14:58:56Z"
tags: []
categories: ["Web前端"]
slug: "attr-checked-true"
draft: false
lastmod: "2014-11-12T14:58:56Z"
id: 2154966
url: "https://i-conquer.iteye.com/blog/2154966"
---

我在用.attr(“checked”, true)发现无法设置checkbox的状态，第一次使用 .attr(“checked”, true) 的时候, checkbox被正确的勾选上了, 但是如果取消了勾选状态(手动取消, .removeAttr(“checked”), 或.attr(“checked”, false)中的任何一种方法), 再次使用 .attr(“checked”, true) 或 .attr(“checked”, “checked”) 都将没有任何效果.原来jQuery 1.9 以后, 使用 .attr(“checked”, true) 或  attr(“checked”, “checked”) 无法设置 checkbox的状态, 同样的, 使用 .attr(“checked”) 也无法正确获取checkbox的状态

应该使用 .prop(“checked”, true) 和 .prop(“checkbox”) 来设置和获取checkbox的勾选状态, 习惯使用.is(“:checked”) 来获取状态, 这也是推荐的方法.