当我取消删除时,为什么不勾选复选框?
这里是我的完整代码:当我取消删除时,为什么不勾选复选框?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SomuFinance - Personal Finance Manager</title>
<link rel="stylesheet" type="text/css" href="indexStyle.css">
<script src="scripts/jquery-3.1.0.min.js"></script>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="container">
<input type="submit" class="button" name="edit" value="Edit" />
<input type="button" class="button" name="delete" value="Delete" />
<input type="text" id="action" name="action">
<table id="listDB">
<tr>
<th>Select</th>
<th>ID</th>
<th>Category ID</th>
<th>Shop</th>
<th>Item</th>
<th>Quantity</th>
<th>Unit</th>
<th>Price Based On</th>
<th>MRP</th>
<th>Seller's Price</th>
<th>Last Updated On</th>
</tr>
<?php
$dbc = mysqli_connect('localhost','root','atlantis2016','itemDB')
or die("Error Connecting to Database");
if(isset($_POST['confirmDelete']))
{
if($_POST['action']=='confirmDelete')
{
foreach ($_POST['selected'] as $delete_id)
{
$query = "DELETE FROM grocery WHERE id = $delete_id";
mysqli_query($dbc, $query)
or die('Error querying database.');
}
}
}
$query1 = "SELECT DISTINCT category FROM grocery";
$result1 = mysqli_query($dbc, $query1)
or die("Error Querying Database");
while($row = mysqli_fetch_array($result1))
{
$category = $row['category'];
$query2 = "SELECT * FROM grocery WHERE category='$category' ORDER BY item ASC";
$result2 = mysqli_query($dbc, $query2)
or die("Error Querying Database");
echo '<tr>';
echo '<td class="catHead" colspan=11>'.$category.'</td>';
echo '</tr>';
$catCount=1;
while($inRow = mysqli_fetch_array($result2))
{
$id = $inRow['id'];
$shop = $inRow['shop'];
$item = $inRow['item'];
$qnty = $inRow['quantity'];
$unit = $inRow['unit'];
$price_based_on = $inRow['price_based_on'];
$mrp = $inRow['MRP'];
$sellers_price = $inRow['sellers_price'];
$last_updated_on = $inRow['last_updated_on'];
echo '<tr>';
echo '<td><input type="checkbox" id="selected" value="' . $id . '" name="selected[]" /></td>';
echo '<td>'.$id.'</td>';
echo '<td>'.$catCount.'</td>';
echo '<td>'.$shop.'</td>';
echo '<td class="leftAligned">'.$item.'</td>';
echo '<td>'.$qnty.'</td>';
echo '<td>'.$unit.'</td>';
echo '<td>'.$price_based_on.'</td>';
echo '<td class="pri">₹'.$mrp.'</td>';
echo '<td class="pri">₹'.$sellers_price.'</td>';
echo '<td>'.$last_updated_on.'</td>';
echo '</tr>';
$catCount++;
}
}
mysqli_close($dbc);
?>
</table>
</div>
<div class="dialogBG">
<div id="deleteConfirmDialog" class="dialog">
<div class="closeDialog"></div>
<p>Sure you want to delete the selected Data?</p>
<input type="submit" id="confirmDelete" class="dialogButton" name="confirmDelete" value="Delete" />
<input type="button" id="cancelDelete" class="dialogButton cancelButton" name="cancelDelete" value="Cancel" />
</div>
</div>
</form>
<script type="text/javascript">
$(document).ready(function(){
$('.button').click(function(){
if($(this).val()=="Delete")
{
$(".dialogBG").fadeIn(200);
$("#deleteConfirmDialog").show(200);
$("#action").val('confirmDelete');
}
else if($(this).val()=="Edit")
{
}
});
$('#confirmDelete').click(function(){
$(".closeDialog").trigger("click");
});
$('#cancelDelete').click(function(){
$("checkbox[name='selected[]']").attr('checked', false);
});
$(".closeDialog").click(function (e){
$(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
});
$(".cancelButton").click(function (e){
$(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
});
$("form").submit(function(e){
alert("Form is being sumbitted!");
});
});
</script>
</body>
</html>
问题部分是:(在PHP)
echo '<td><input type="checkbox" id="selected" value="' . $id . '" name="selected[]" /></td>';
(在HTML)
<input type="button" id="cancelDelete" class="dialogButton cancelButton" name="cancelDelete" value="Cancel" />
(jQuery中)
$('#cancelDelete').click(function(){
$("checkbox[name='selected[]']").attr('checked', false);
});
何我是否确保在出现的弹出窗口中单击取消按钮时取消选中复选框? I.e.如何确保名为selected []的复选框([]是用于PHP的目的)在单击#cancelDelete时未被选中?
在您的取消按钮添加下面的jQuery关闭之前弹出
$('.myform input:checkbox').prop('checked', false);
这将设置所有选中。
例子:
$("#cancelButton").click(function() {
$('.myform input:checkbox').prop('checked', false);
// close the popup
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="myform">
<input type="checkbox">Item 1
<br>
<input type="checkbox">Item 2
<br>
<input type="checkbox">Item 3
<br>
<br>
<button id="cancelButton">Cancel</button>
</div>
更改选择以下几点:
$('#cancelDelete').click(function(){
$("input[type='checkbox']:checked").attr('checked', false);
});
删除'type = checkbox'的那些双引号 – iHasCodeForU
您可以添加使用removeAttr
代替:
$('#cancelDelete').click(function(){
$("input:checkbox[name='selected[]']").removeAttr('checked');
});
希望这有助于。
$('#cancelDelete').click(function(){
$("input:checkbox[name='selected[]']").removeAttr('checked');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' name='selected[]' checked>
<input type='checkbox' name='selected[]' />
<input type='checkbox' name='selected[]' checked/>
<input type='checkbox' name='selected[]' />
<button id='cancelDelete'>Cancel</button>
@Somenath Sinha在你OP OP问题是_如何确保名为**选中的复选框[] ** _我认为接受的帖子应该覆盖这部分也不是? –
如果您有jQuery的问题,你为什么要张贴了很多PHP的?请点击''''按钮并创建一个[mcve] – mplungjan
@mplungjan突出显示问题部分。还包括整个代码,如果有什么东西可以逃脱我的注意力,这对解决问题是有效的。 –
但我们不能运行它! – mplungjan