I am working with jQuery and this is my code so far:
$('.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');
}
});
});
Here is my 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>
I have several checkboxes which i use as categories. These categories have sub-categories, so this code is simply highlighting the checkboxes which are selected. If the selected checkbox is from mother category it highlights all the cheboxes for all children categories. Also setting them as checked
.
Everything i achieved so far as i want. Right now i am stuck about how can i get the id of the selected checkbox and add it into variable looking like this:
var SelectedCategoryIDs = '12,435,123,124,56,34,23';
So when i click a mother category it will add to SelectedCategoryIDs
the ids of all children categories. Also when i uncheck already checked mother category it will remove all the ids from SelectedCategoryIDs
which corresponds to all children categories.
So can you help me achieve that?
I hope i explained it well, if you have any questions please let me know.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire