为什么* [选中]与单个选中的输入框不匹配? [英] Why does *[checked] not match a single checked input box?

查看:93
本文介绍了为什么* [选中]与单个选中的输入框不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个id为'test'的复选框,并且它在一个加载了jQuery 1.5.2的页面上。

Lets say I have a checkbox with id 'test' and its on a page loaded with jQuery 1.5.2.

<input id="test" type="checkbox"/>

现在,如果我点击复选框将其设置为已选中州......

Now if I click on the checkbox to set it in the checked state...

为什么 $(#test)。is(:checked) return true $(#test)。is(* [checked])返回 false

Why does $("#test").is(":checked") return true and $("#test").is("*[checked]") return false.

这是 js使用描述的场景

推荐答案

[checked] 是一个选择器,用于选择当前已检查的元素 选中属性而非属性 - 换句话说如果元素具有checked属性,请将其选中。

The [checked] is a selector that picks elements that are currently checked with the checked attribute not the property - in other words "if the element has the checked attribute, pick it up".

如果要检查是否选中了复选框,则必须使用 :已选中

If you want to check if the checkbox is checked or not, you must use :checked.

用法示例:

if($('#test').is(':checked'))
{
   return true;
}

使用jQuery 1.6及更高版本,您可以使用 .prop() 出于性能原因的功能。

With jQuery 1.6 and higher you can use the .prop() function for performance reasons.

if($('#test').prop('checked'))
{
   return true;
}

这篇关于为什么* [选中]与单个选中的输入框不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆