javascript - How to get value from Multi CheckBox with the same name -
hello guys , hope not stupid question.
i have multi checkbox same attr name
"check"
every checkbox has deferent value
so have create jquery code if press on checkbox thing in php code
$(document).ready(function(){ var newspaperid; var userid; var categoryid; $("input[type='checkbox']").click(function() { if ($(this).is(':checked')) { newspaperid= $('input[name="check"]:checked').val(); userid= $(".userid").val(); categoryid= $(".categoryid").val(); alert(newspaperid); $.post("addanddelete.php", { add:1, userid: userid, newspaperid: newspaperid, categoryid:categoryid }, function(data, status){ alert("data: " + data + "\nstatus: " + status); }); } else { newspaperid= $('input[name="check"]:checked').val(); userid= $(".userid").val(); categoryid= $(".categoryid").val(); alert(newspaperid); $.post("addanddelete.php", { delete:1, userid: userid, newspaperid: newspaperid, categoryid:categoryid }, function(data, status){ alert("data: " + data + "\nstatus: " + status); }); } }); });
and here checkbox code created php
<?php $i=1; $sql = "select newspaperstatus.*,userchoises.*" . " newspaperstatus" . " left join userchoises" . " on newspaperstatus.id=userchoises.newspaperid" . " newspaperstatus.categoryid=$categoryid"; $query = mysql_query($sql); while ($row = mysql_fetch_assoc($query)){ if($i==1){ if($row['id']==$row['newspaperid']){ echo '<tr><th><img src="../images/newspaper/'.$row['logo'].'"/></th><th><a href="">'.$row['name'].'</a></th><th><input class="check" type="checkbox" checked="checked" name="check" value="'.$row['id'].'"/></th>'; }else{ echo '<tr><th><img src="../images/newspaper/'.$row['logo'].'"/></th><th><a href="">'.$row['name'].'</a></th><th><input class="check" type="checkbox" name="check" value="'.$row['id'].'"/></th>'; } } else if($i==2){ if($row['newspaperid']==$row['id']){ echo '<th><img src="../images/newspaper/'.$row['logo'].'"/></th><th><a href="">'.$row['name'].'</a></th><th><input class="check" type="checkbox" checked="checked" name="check" value="'.$row['id'].'"/></th>'; }else{ echo '<th><img src="../images/newspaper/'.$row['logo'].'"/></th><th><a href="">'.$row['name'].'</a></th><th><input class="check" type="checkbox" name="check" value="'.$row['id'].'"/></th>'; } } else if($i==3){ if($row['newspaperid']==$row['id']){ echo '<th><img src="../images/newspaper/'.$row['logo'].'"/></th><th><a href="">'.$row['name'].'</a></th><th><input class="check" type="checkbox" checked="checked" name="check" value="'.$row['id'].'"/></th></tr>'; }else{ echo '<th><img src="../images/newspaper/'.$row['logo'].'"/></th><th><a href="">'.$row['name'].'</a></th><th><input class="check" type="checkbox" name="check" value="'.$row['id'].'"/></th></tr>'; } $i=0; } $i++; } ?>
so problem when press on check box work when unchecked on take last vale of check box (( if press on checkbox value 16 , press on checkbox value 17 work when want uncheck checkbox of value 16 value 17 take last value of checkbox checked
please me 4 days , cant resolve problem.
in below line:
if ($(this).is(':checked')) { newspaperid= $('input[name="check"]:checked').val();
you taking value of checked checkbox , have put condition of checked in if condition. suggestion replace
newspaperid= $('input[name="check"]:checked').val();
above line following line:
newspaperid= $(this).val();
and in else part need check if there checked checkbox or not. here code it:
else { $('input[name="check"]').each(function(){ if($(this).is(':checked')){ newspaperid= $(this).val(); } }); if(newspaperid != ''){ //do stuff}
here reason make if condition. reason if user click on checked checkbox , other checkbox not checked value of newspaperid
blank else if take value of other checkbox checked.
Comments
Post a Comment