我有网格,每行添加一个复选框。当我单击删除按钮而不单击复选框时,它会显示“请选择至少一行来执行”。单击复选框时也会显示相同的消息。请帮助我找出我在下面提到的代码中所做的解决方案和错误。
//Calling Delete function clicking on checkbox
$(document).ready(function () {
//Grid Function
$.ajax(
{
url: "GetCompanyTypeMasterDetails",
type: "get",
dataType: "json",
contentType: "application/json",
success: function (data) {
$("#grid").igGrid({
autoGenerateColumns: false,
primaryKey: "CompanyTypeID",
columns: [
{ headerText: "Company Type ID", key: "CompanyTypeID", dataType: "Numeric", hidden: true },
{
headerText: "", key: "CompanyTypeID", dataType: "Numeric", width: "80",
template: '<table><tr><td><input type="image" src="/Images/GridImages/edit.png" onclick = "EditRecords(${CompanyTypeID})" /></td><td><input type="image" src="/Images/GridImages/delete.png" onclick = "DeleteRecords(${CompanyTypeID})" /></td></tr></table>'
},
{ headerText: "", key:"CompanyTypeID", dataType:"Numeric", width:"50", template: '<input type="checkbox" id="chkID" />'},
{ headerText: "Company Type", key: "CompanyTypeName", dataType: "string", width: "600" },
{ headerText: "Company Code", key: "Code", dataType: "string", width: "600" },
dataSource: data}
});
$("#btnDelete").click(function (ID) {
var isSelected = $('#chkID').is(':checked');
if (!isSelected) {
alert("Please select atleast one row to perform this action");
}
else if (isSelected) {
DeleteRecords(ID);
}
});
});
});
//Delete Function
function DeleteRecords(ID) {
$.ajax(
{
url: "DeleteRecords",
type: "post",
dataType: "json",
data: {CompanyTypeID : ID},
success: function (data) {
alert("Are you sure you want to Delete?");
location.reload();
},
error: function (status) {
alert(status.responseText);
}
});
}
答案 0 :(得分:0)
$('#chkID')
指的是id = chkID的元素。我认为你在这里遇到的问题是你有多个具有相同id的复选框。
可能的解决方案可能是:向所有复选框添加一个类(让我们说'chkBox'):
$("#btnDelete").click(function (ID) {
if ($('.chkBox:checked').length == 0) {
alert("Please select atleast one row to perform this action");
}
else {
DeleteRecords(ID);
}
});
这是有效的Fiddle