vendredi 3 juin 2016

Getting the checkbox value via jquery

<?php foreach($active_brand as $brand) { ?>
    <input class="my-checkbox"  value="<?=$brand['id']?>" type="checkbox" <?php if($brand['active']==1) { echo "checked data-toggle='toggle'";} else {echo "data-toggle='toggle' class='off'"; }  ?>>
    <input class="brandActive" id="brandActive" type="hidden" value="<?=$brand['active']?>">
    <?php } ?>

I'm trying to access the checkbox value on change function like this in jquery:

 <script>
        $(document).ready(function() {
            $('.my-checkbox').change(function() {
          /*      var brandStatus = 0;
                if($('.brandActive').val()==0)
                {
                    brandStatus=1;
                }
                else{
                    brandStatus=0;
                }*/
                alert($(this).val()); // this value is ok 
                alert($('.brandActive').val()); // this values => not ok
            });
        });
    </script>

The value printed out in:

 $('.brandActive').val()

is always 0... So I was wondering if I can reach the value of the checkbox somehow within the checkbox itself... There has to be a way I can do this, can someone help me out??

Thanks! :)




Aucun commentaire:

Enregistrer un commentaire