我有一个 grdiview,其中添加了多重删除功能。请参阅代码供您引用:-
<script type="text/javascript">
function ValidateAll() {
var chkselectcount = 0;
var gridview = document.getElementById('<%= grdTeacherProfile.ClientID %>');
for (var i = 0; i < gridview.getElementsByTagName("input").length; i++) {
var node = gridview.getElementsByTagName("input")[i];
if (node != null && node.type == "checkbox" && node.checked) {
chkselectcount = chkselectcount + 1;
}
}
if (chkselectcount == 0) {
alert("Please select atleast One CheckBox");
return false;
}
else {
ConfirmationBox();
}
}
function ConfirmationBox() {
var result = confirm("Are you sure, you want to delete the Users ?");
if (result) {
return true;
}
else {
return false;
}
}
</script>
另请参阅按钮 html:-
<asp:Button ID="btnDelete" runat="server" CausesValidation="false" CssClass="btn btn-danger" Text="Delete" OnClick="btnDelete_Click" OnClientClick="javascript:return ValidateAll();" />
问题是,
当我选中复选框并单击删除按钮时,它会要求确认。当我单击“取消”时,它仍然会从 Gridview
以及 sql 表
中删除该行。
我应该做什么才能使其正常工作。 ?请推荐
最佳答案
我认为你需要使用
return ConfirmationBox();
而不是
ConfirmationBox();
所以你的代码变成了
function ValidateAll() {
var chkselectcount = 0;
var gridview = document.getElementById('<%= grdTeacherProfile.ClientID %>');
for (var i = 0; i < gridview.getElementsByTagName("input").length; i++) {
var node = gridview.getElementsByTagName("input")[i];
if (node != null && node.type == "checkbox" && node.checked) {
chkselectcount = chkselectcount + 1;
}
}
if (chkselectcount == 0) {
alert("Please select atleast One CheckBox");
return false;
}
else {
return ConfirmationBox();
}
}
您需要从 OnClientClick
中删除 javascript:
,您可以使用 OnClientClick="return ValidateAll();"
关于javascript - 单击按钮时的多重删除复选框无法按要求工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27653674/