ho un multi casella di controllo con la stessa attr name
come "Check"
ma ogni opzione ha un valore diversoCome ottenere valore da Multi CheckBox con lo stesso nome
Quindi devo creare un codice jQuery che se premo sul casella di controllo farà qualche cosa in PHP come questo codice
$(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);
});
}
});
});
e qui è il codice casella di controllo creato da php
<?php
$i=1;
$sql = "SELECT NewsPaperStatus.*,UserChoises.*"
. " FROM NewsPaperStatus"
. " LEFT JOIN UserChoises"
. " ON NewsPaperStatus.Id=UserChoises.NewsPaperId"
. " WHERE 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++;
}
?>
in modo che il il problema è quando premo su qualsiasi casella di controllo funziona bene, ma quando ho deselezionato un altro su di esso, prendo l'ultimo valore di casella di controllo ((se premo il valore 16 di casella di controllo e premo il valore di casella 17 così funziona bene ma quando voglio deseleziona la casella di controllo del valore 16 il valore è 17 e prende l'ultimo valore della casella di controllo che ho controllato.
Non è chiaro su cosa vuoi ottenere e dove stai avendo problemi. – Lupin