>我有一个从数据库获取值的动态表。我有一个按钮,onclcik 运行查询以从数据库中删除记录。目前不是删除记录(单击(,而是删除最后一条可用的记录。我从这里更新,当我向帖子提供ID时,我发现了问题,以某种方式将最后一个ID发送到查询,无论如何我都可以删除所选的ID。
//this is how im creating dynamic table
//just for the test
function() {
});
});
<table>
<thead>
<tr>
<td>
</td>
</tr>
</thead>
<tbody id="table"></tbody>
</table>
<button type="button" id="chochk" style="display:none" class="sukuti">delete</button>
.
也许您可以更好地直接侦听复选框的change
事件,然后获取event.target
的值?我怀疑您在代码中获取变量ad
的方式有问题,但是如果不了解它是如何派生的,就很难看出原因。下面的示例显示了获取复选框值的简单方法,希望这可能会引导您找到正确的答案。
$('.test').on('change', function(e) {
alert(e.target.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Value of 1: <input type="checkbox" class="test" value="1" /><br />
Value of 2: <input type="checkbox" class="test" value="2" />
使用单选按钮代替复选框,因此用户一次只能选择一个。然后在删除按钮的处理程序中,获取选中按钮的值并将其发送到服务器。
//this is how im creating dynamic table
//just for the test
function() {
var table = document.getElementById('tablebody');
for (var i = 0; i < (test.length); i = i + 2) { //test is an array of values from db
var row = table.insertRow(-1);
var cell3 = row.insertCell(-1);
var cell1 = row.insertCell(-1);
var cell2 = row.insertCell(-1);
cell1.innerHTML = //some html span
cell2.innerHTML = //some html span
//this is the checkbox which onclick shows the button
cell3.innerHTML = '<input type="radio" name="checkbox" class="your" value="ID of current row">';
}
///this is how i am displaying a button on click and also delete function
$(document).on('click', ".your", function() {
$('#chochk').show();
});
$(".sukuti").click(function() {
var ad = $(".your:checked").val();
$.post("test.jsp", {
id: ad //ad is the ID which will delete the specifIC ID being clicked
}, function(data) { //sending a query to delete from db working good
});
});
<table>
<thead>
<tr>
<td>
</td>
</tr>
</thead>
<tbody id="table"></tbody>
</table>
<button type="button" id="chochk" style="display:none" class="sukuti">delete</button>