【发布时间】:2016-03-24 05:26:54
【问题描述】:
我正在使用 jQuery,这是我目前的代码:
$('.CategoryInput').each(function() {
$(this).click(function() {
var CategoryID = $( this ).attr( "category" );
var Clicked = $( this ).attr( "clicked" );
if(Clicked == 0){
$("#Category" + CategoryID).css({backgroundColor: '#fc3b66'});
$("#Category" + CategoryID).find( ".state" ).css({color: '#fff'});
$(".subcat" + CategoryID).css({backgroundColor: '#fc3b66'});
$(".subcat" + CategoryID).find( ".state" ).css({color: '#fff'});
$(".SubCategoryInput" + CategoryID).prop('checked', true);
$(".SubCategoryInput" + CategoryID).attr('clicked','1');
$(this).attr('clicked','1');
} else {
$("#Category" + CategoryID).css({backgroundColor: '#fdfdfd'});
$("#Category" + CategoryID).find( ".state" ).css({color: '#6a6d76'});
$(".subcat" + CategoryID).css({backgroundColor: '#fdfdfd'});
$(".subcat" + CategoryID).find( ".state" ).css({color: '#6a6d76'});
$(".SubCategoryInput" + CategoryID).prop('checked', false);
$(".SubCategoryInput" + CategoryID).attr('clicked','0');
$(this).attr('clicked','0');
}
});
});
这是我的 HTML:
<div class="container">
<h1>Selecciona los productos que elaboras:</h1>
<?PHP
$r = mysql_query("SELECT * FROM `Category` WHERE `subCategory`='0' ORDER by Position ASC");
while($rowi = mysql_fetch_array($r))
{
$id = $rowi['id'];
$Name = $rowi['Name'];
$Description = $rowi['Description'];
?>
<div class="row">
<div class="maintag" id="Category<?PHP echo $id;?>">
<label class="state" for="categories"><?PHP echo $Name;?></label>
<input type="checkbox" class="CategoryInput" name="categories" category="<?PHP echo $id;?>" clicked="0">
(<?PHP echo $Description;?>)
</div>
</div>
<div class="row">
<?PHP
$r1 = mysql_query("SELECT * FROM `Category` WHERE `subCategory`='$id' ORDER by Position ASC");
while($rowi1 = mysql_fetch_array($r1))
{
$id1 = $rowi1['id'];
$Name1 = $rowi1['Name'];
?>
<div class="col-md-4" style="padding-left:0px;">
<div id="subtag<?PHP echo $id1;?>" class="subcat<?PHP echo $id;?> supersub">
<label class="state" for="categories"><?PHP echo $Name1;?></label>
<input type="checkbox" name="subcategory" class="SubCategoryInput<?PHP echo $id;?>" SubCategory="<?PHP echo $id1;?>" clicked="0">
</div>
</div>
<?PHP } ?>
</div>
<?PHP } ?>
</div>
我有几个复选框用作类别。这些类别有子类别,因此此代码只是突出显示选中的复选框。如果选中的复选框来自母类别,它会突出显示所有子类别的所有复选框。还将它们设置为checked。
到目前为止,我所取得的一切都是我想要的。现在我被困在如何获取所选复选框的 id 并将其添加到如下所示的变量中:
var SelectedCategoryIDs = '12,435,123,124,56,34,23';
因此,当我单击一个母类别时,它会将所有子类别的 ID 添加到 SelectedCategoryIDs。此外,当我取消选中已选中的母亲类别时,它会从 SelectedCategoryIDs 中删除所有与所有子类别相对应的 ID。
那么你能帮我实现吗?
希望我解释得很好,如果您有任何问题,请告诉我。
【问题讨论】:
-
你能给我们看看你的 HTML 吗?
-
完成,查看我更新的问题。
标签: javascript jquery checkbox