Check all just select the first value of foreach

2

I tried to do a test of the submit that deletes the scenes only to appear when clicking on the checkboxes, but the same thing happens, it only appears if you select the first one. But if you select the first one and a few more it deletes in the same, but only appeared if the first one was selected ..

<form action="" method="post">
<? foreach($itens as $myrow){ ?>
<a href="pagina.php?ID=<? echo $myrow['ID']; ?>"><? echo $myrow['Title']; ?></a>


<? echo $myrow['GDate']; ?>
?>
<? echo $myrow['Hour']; ?>
<div class="dashed-line"></div>
<input name="selector[]" id="testecb" type="checkbox" value="<?php echo $myrow['ID']; ?>" />
</div>
</div>
<?}?>
<input type="button" id="selectall-game-button" label="check all" value="Selecionar tudo"
><input type="submit" id="delete-game-button" value="Eliminar" />

<?
****************** AQUI ESTA A ACÇÃO QUE DELETA.. **************
}
} ?>

JS:

$('#selectall-game-button').click(function(){
        var chk = $(this).click('checked')?true:false;
        $('#testecb').attr('checked',chk);
});

Does anyone have any hates why this happens? : ss

    
asked by anonymous 30.01.2015 / 19:39

1 answer

2

You have two problems: Multiple IDs and an error in JS.

# 1 - You can not use multiple IDs in this case. CSS classes is the solution. This is because the $('#testecb') selector will only return the first element it finds. I suggest you use $ ('. dashed-line input')

# 2 - In JS you have $(this).click('checked') but this element is an input and does not have checked . I guess then this input type="button" should check all the checkboxes ...

So one suggestion would be:

$('#selectall-game-button').click(function(){
    $('[name="selector[]"]').prop('checked', true);
});

Note that this solution does not fix the multiple IDs you are generating in PHP. To fix this you could use it in PHP:

<div class="dashed-line"></div>
<input name="selector[]" class="testecb" type="checkbox" value="<?php echo $myrow['ID']; ?>" />
</div>

and then the selector could already be $('.testecb').prop('checked', true);

    
30.01.2015 / 19:47