jeudi 5 octobre 2017

Proper jQuery selector for html elements with array name

I have multiple check boxes in my code

 <label><input name="meta[Radio & Speakers]" class="flat" type="checkbox" value="YES"> Radio & Speakers</label>

Whenever the checkboxes are unchecked, a hidden input is put before the unchecked checkbox with the same name using:

$('input').on('ifUnchecked', function(event){
            name = $(this).attr('name');

            if($('input[type="checkbox"][name="'+name+']"').length == 0){
                $(this).before('<input type="hidden" name="'+name+'" value="NO"/>');
            }

        });  

I would like to avoid multiple insertions of the hidden input by checking if it exists using $('input[type="checkbox"][name="'+name+']"').length but it seems to fail, There's something I'm missing out here ...

Manually adding hidden check boxes is not feasible for my scenario since they are so many.




Aucun commentaire:

Enregistrer un commentaire