JQuery检查是否未选中复选框 [英] JQuery check if checkbox is NOT checked

查看:98
本文介绍了JQuery检查是否未选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法搞清楚这一点。我有两个复选框(将来会有更多):

I'm having trouble figuring this out. I have two checkboxes (in the future will have more):


checkSurfaceEnvironment-1

checkSurfaceEnvironment-2

checkSurfaceEnvironment-1
checkSurfaceEnvironment-2

基本上,我想写一个if语句并测试是否选中其中一个。最简单的方法是什么? (即如果检查了checkSurfaceEnvironment-1并且checkSurfaceEnvironment-2不是......做某事)

Basically, I want to write an if statement and test if either are checked. What's the easiest way to accomplish this? (i.e if checkSurfaceEnvironment-1 is checked and checkSurfaceEnvironment-2 is NOT.. do something)

if ( $("#checkSurfaceEnvironment-1").attr('checked', true) && $("#checkSurfaceEnvironment-2").is('**(NOT??)** :checked') )


推荐答案

我使用的一种可靠方式是:

One reliable way I use is:

if($("#checkSurfaceEnvironment-1").prop('checked') == true){
    //do something
}

如果要迭代已检查的元素,请使用父元素

If you want to iterate over checked elements use the parent element

$("#parentId").find("checkbox").each(function(){
    if ($(this).prop('checked')==true){ 
        //do something
    }
});

更多信息:

这很有效因为所有复选框都选中了一个属性,该属性存储了复选框的实际状态。如果您希望可以检查页面并尝试选中并取消选中一个复选框,您会注意到已检查属性(如果存在)将保持不变。此属性仅表示复选框的初始状态,而不是当前状态。当前状态存储在该复选框的dom元素选中的属性中。

This works well because all checkboxes have a property checked which stores the actual state of the checkbox. If you wish you can inspect the page and try to check and uncheck a checkbox, and you will notice the attribute "checked" (if present) will remain the same. This attribute only represents the initial state of the checkbox, and not the current state. The current state is stored in the property checked of the dom element for that checkbox.

参见 HTML中的属性和属性

这篇关于JQuery检查是否未选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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