Jquery 检查';这';复选框已选中
如何确定是否已使用this命令选中了“this”复选框 我有以下内容,但不确定如何实现这一点(因此它只检查选中的单个复选框,而不是页面上的所有复选框)Jquery 检查';这';复选框已选中,jquery,checkbox,Jquery,Checkbox,如何确定是否已使用this命令选中了“this”复选框 我有以下内容,但不确定如何实现这一点(因此它只检查选中的单个复选框,而不是页面上的所有复选框) if ($('input[type="checkbox"]').is(':checked') 这个怎么样 if($(this).is(':checked')) 您可能是指事件处理程序上下文中的this,在这种情况下,只需测试checked属性: $('input[type="checkbox"]').click(function() {
if ($('input[type="checkbox"]').is(':checked')
这个怎么样
if($(this).is(':checked'))
您可能是指事件处理程序上下文中的
this
,在这种情况下,只需测试checked
属性:
$('input[type="checkbox"]').click(function() {
if (this.checked) {
// checkbox clicked is now checked
}
});
请注意,您可以使用以下方法执行相同的操作:
$(this).is(':checked')
$(this.prop('checked')
this.checked
避免创建新的jQuery对象,因此效率更高(编码速度更快)。is
是最慢的:它需要的时间是prop
的两倍,大约是简单属性查找的100倍
您需要使用ID选择器。输入[type=“checkbox”]选择器将返回“所有复选框”
if ($('#Checkbox1').is(':checked'))
其中Checkbox1是复选框的ID请尝试以下您知道ID的方法
$('#edit-checkbox-id').is(':checked');
或者,如果您已使用此关键字引用项目
$(this).is(':checked');
我使用以下方法:
$(this).find('input[type="checkbox"]:checked')
此示例返回选中的所有复选框您的意思是这样的(0是索引):
或者像这样:
if ($(this).is(':checked'))
查看prop()
:
这些文档提供了多种更好的方法来检查“已选中”复选框
elem.checked | true (Boolean) Will change with checkbox state
$(elem).prop("checked") | true (Boolean) Will change with checkbox state
elem.getAttribute("checked") | "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6) | "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6.1+) | "checked" (String) Will change with checkbox state
$(elem).attr("checked")(pre-1.6) | true (Boolean) Changed with checkbox state
这是可行的,但这不是一个好的做法。请参阅我的答案,了解为什么这是非常低效的。
elem.checked | true (Boolean) Will change with checkbox state
$(elem).prop("checked") | true (Boolean) Will change with checkbox state
elem.getAttribute("checked") | "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6) | "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6.1+) | "checked" (String) Will change with checkbox state
$(elem).attr("checked")(pre-1.6) | true (Boolean) Changed with checkbox state