Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery代码在选中复选框但不';如果不检查,则无法工作_Jquery_Wordpress_Advanced Custom Fields_Gutenberg Blocks - Fatal编程技术网

jQuery代码在选中复选框但不';如果不检查,则无法工作

jQuery代码在选中复选框但不';如果不检查,则无法工作,jquery,wordpress,advanced-custom-fields,gutenberg-blocks,Jquery,Wordpress,Advanced Custom Fields,Gutenberg Blocks,我有一个jQuery函数,当它被选中时,它会改变的颜色。如果未选中,则没有背景。检查后,它应该是黑色的。它在选中时工作,但在取消选中时不工作 这就是我到目前为止所做的: $(document).on('change', '#two_img_left_feature_img label.selected input', function () { if ($("#two_img_left_feature_img label.selected input").is(&qu

我有一个jQuery函数,当它被选中时,它会改变
的颜色。如果未选中,则没有背景。检查后,它应该是黑色的。它在选中时工作,但在取消选中时不工作

这就是我到目前为止所做的:

$(document).on('change', '#two_img_left_feature_img label.selected input', function () {
    if ($("#two_img_left_feature_img label.selected input").is(":checked")) {
        $('#two_img_left_feature_img').css('background','black');
    } else {
        $('#two_img_left_feature_img').css('background','white');
    }
});
HTML很简单:

<div id="two_img_left_feature_img">
     <input type="checkbox" id="acf-block_60173d68d2c6a-field_5f8cf27e7c048-two_img_left_set_featured_img" name="acf-block_60173d68d2c6a[field_5f8cf27e7c048][]" value="two_img_left_set_featured_img">
</div>

问题是关于
标签的。已选择
。。。如果未选中该复选框,我猜标签没有选中的
.selected

因此,只需从事件处理程序选择器和条件中删除
label.selected
。只是太具体了

$(文档)。在(“单击“,”两个img左功能输入“,”功能(){
如果($(“#2#img_left_feature_img input”)。是(“:checked”)){
$(“#两个图片"左"特征"图片”).css(“背景”、“黑色”);
}否则{
$(“#两个图片"左"特征"图片”).css(“背景”、“白色”);
}
});
#两个img(左)特征(左){
高度:200px;
宽度:200px;
}

ahhh,太简单了!谢谢