如果选中了复选框,如何在jQuery中进行检查,并在“";单击();功能?

如果选中了复选框,如何在jQuery中进行检查,并在“";单击();功能?,jquery,Jquery,我有这个代码,我需要设置条件来检查我的复选框是否被选中 HTML: 使用选中的属性 $('#checkbox_group input[type=checkbox]').click(function() { if (this.checked) { $(this).siblings('input[type=checkbox]').prop('checked', false); } else { // if is NOT checked

我有这个代码,我需要设置条件来检查我的复选框是否被选中

HTML:

使用选中的属性

$('#checkbox_group input[type=checkbox]').click(function() {    
    if (this.checked) {
        $(this).siblings('input[type=checkbox]').prop('checked', false);
    }
    else {
        // if is NOT checked then check it liek checked="true"
        $(this).siblings('input[type=checkbox]').prop('checked', true);
    }
});
$('#checkbox_group input[type=checkbox]').click(function() {    
    if (this.checked) 
        $(this).siblings().prop('checked', false);  
    else        
        $(this).siblings().prop('checked', true);    
});
如果只有需要参与的同级复选框,则可以忽略同级中的选择器

$('#checkbox_group input[type=checkbox]').click(function() {    
    if (this.checked) {
        $(this).siblings('input[type=checkbox]').prop('checked', false);
    }
    else {
        // if is NOT checked then check it liek checked="true"
        $(this).siblings('input[type=checkbox]').prop('checked', true);
    }
});
$('#checkbox_group input[type=checkbox]').click(function() {    
    if (this.checked) 
        $(this).siblings().prop('checked', false);  
    else        
        $(this).siblings().prop('checked', true);    
});
使用选中的属性

$('#checkbox_group input[type=checkbox]').click(function() {    
    if (this.checked) {
        $(this).siblings('input[type=checkbox]').prop('checked', false);
    }
    else {
        // if is NOT checked then check it liek checked="true"
        $(this).siblings('input[type=checkbox]').prop('checked', true);
    }
});
$('#checkbox_group input[type=checkbox]').click(function() {    
    if (this.checked) 
        $(this).siblings().prop('checked', false);  
    else        
        $(this).siblings().prop('checked', true);    
});
如果只有需要参与的同级复选框,则可以忽略同级中的选择器

$('#checkbox_group input[type=checkbox]').click(function() {    
    if (this.checked) {
        $(this).siblings('input[type=checkbox]').prop('checked', false);
    }
    else {
        // if is NOT checked then check it liek checked="true"
        $(this).siblings('input[type=checkbox]').prop('checked', true);
    }
});
$('#checkbox_group input[type=checkbox]').click(function() {    
    if (this.checked) 
        $(this).siblings().prop('checked', false);  
    else        
        $(this).siblings().prop('checked', true);    
});
Try
is(':checked')

Try
is(':checked')



这有什么意义?这就是默认情况下复选框的工作方式(没有任何JavaScript)。我也在期待它,但我正在使用Wordpress,出于某种原因,它不能正常工作。它在一个插件中,所以我需要找到一个解决方法。这类似于错误日期()php函数的WP问题,当您需要正确的WP日期/时间时,您需要使用date_i18n()函数。可能有一个元素覆盖了表单元素,因此单击无法到达它们?
数据
似乎与一些PHP最佳实践有关,与基本HTML元素的工作方式无关。我怀疑Wordpress在禁用表单元素方面做了些什么。这有什么意义?这就是默认情况下复选框的工作方式(没有任何JavaScript)。我也在期待它,但我正在使用Wordpress,出于某种原因,它不能正常工作。它在一个插件中,所以我需要找到一个解决方法。这类似于错误日期()php函数的WP问题,当您需要正确的WP日期/时间时,您需要使用date_i18n()函数。可能有一个元素覆盖了表单元素,因此单击无法到达它们?
数据
似乎与一些PHP最佳实践有关,与基本HTML元素的工作方式无关。我怀疑Wordpress会禁用表单元素。我也会这样做。谢谢,你能添加添加
checked=“true”
checked=“false”
的代码吗?我也会这样做。谢谢,你能添加添加
checked=“true”
checked=“false”
的代码吗?谢谢,但是它不会像
this.setAttribute('checked',this.checked')那样将
checked=“true”
添加到html中可以。而且它只能从许多中选择一个。我需要能够选择多个复选框。知道怎么做吗?@Danteonline:当用户与页面交互时,HTML永远不会改变。虽然您可以更改元素的属性,但更改属性就足够了。但是缺少的一件事是将checked状态分别添加到html中,如
checked=“true”
。你知道怎么做吗?@Danteonline:你为什么要这么做?同样,您不能修改HTML源代码。@FelixKling
this.setAttribute('checked',this.checked)将向代码中添加
checked=“true”
。但是如何添加
checked=“false”
?我需要这些东西来实现我想在我的代码中实现的下一个东西,这些东西将只选择选中了=“true”等的东西。谢谢,但它不会像
this.setAttribute('checked',this.checked)那样将
checked=“true”
添加到html中可以。而且它只能从许多中选择一个。我需要能够选择多个复选框。知道怎么做吗?@Danteonline:当用户与页面交互时,HTML永远不会改变。虽然您可以更改元素的属性,但更改属性就足够了。但是缺少的一件事是将checked状态分别添加到html中,如
checked=“true”
。你知道怎么做吗?@Danteonline:你为什么要这么做?同样,您不能修改HTML源代码。@FelixKling
this.setAttribute('checked',this.checked)将向代码中添加
checked=“true”
。但是如何添加
checked=“false”
?我需要这些东西来实现我想在代码中实现的下一个东西,这些东西将只选择选中了=“true”等的东西。