簡體   English   中英

單擊復選框時如何顯示確認消息

[英]How to display confirmation message when a checkbox is clicked

我希望能夠在單擊 asp.net checkbox時顯示確認消息,我嘗試使用:

OnClientClick = "return confirm('Are you sure you want to logout?')"

它不報告錯誤,但不工作,我后來發現 asp.net 復選框沒有OnClientClick屬性。

我也在google上做了一些研究還是沒能實現! 請問有人知道怎么做嗎? 提前致謝。

經過大量研究,我能夠實現這一點,這是答案,因此面臨相同問題的其他用戶可以使用它:

OnClick="if(!confirm('Are you sure you want to sign out?'))return false;"
這對我來說非常有效。

你可以看看這里

復選框檢查確認消息。

您可以通過使用 javascript,通過檢查復選框的count來做到這一點。

像下面

 if (chkCount === 0) {
    alert("You do not have any selected files to delete.");
    return false;
} else {
    return confirm("Are you sure you want to proceed deleting the selected   files?");
}

希望有幫助

由於你沒有在下面提到的是jquery

  $(document).ready(function() {
        $('#yourelemntid_as_enderedHtml').change(function() {
            if ($(this).prop('checked')) {
                alert("Are you sure you want to logout."); //checked
            }
            else {
                alert("text here"); //not checked
            }
        });
    });

你也最好在EndprocessRequest()上調用它以及 asp 松散這個腳本

在 C# 中

 <asp:CheckBox ID="chkgen" runat="server" Checked='<%# Eval("col3") %>'  
        OnCheckedChanged="chkgen_OnCheckedChanged"/>

protected void chkgen_OnCheckedChanged(object sender, EventArgs e)
{
      int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex;
      CheckBox cb = (CheckBox)gridView.Rows[selRowIndex].FindControl("chkgen");

      if (cb.Checked)
      {
             //Perform your logic
      }
}

你可以在 asp:checkbox 中使用onchange ,就像這樣 -

<asp:CheckBox Text="check this out" runat="server" onchange="return confirm('Are you sure you want to logout?');" />

如果您設置了autopostback = true ,它將回發。

問題在於它適用於每次更改 - 無論是選中還是取消選中,為了緩解這種情況,您可以像這樣使用它。

<asp:CheckBox Text="check this out" runat="server" onchange="return Confirmation(this);" />

function Confirmation(element)
{
    if(element.checked) // so that only for checked case, this will execute.
         return confirm('Are you sure you want to logout?');
    else
         return false;
}

您可以為所有復選框分配功能,然后在其中要求確認。 如果他們選擇yes,則允許更改復選框,否則它保持不變。

在我的情況下,我還使用帶有 Autopostback="True" 屬性的 ASP .Net 復選框,因此在服務器端,我需要比較提交的值與當前在 db 中的值,以便知道他們選擇了什么確認值並更新 db 僅當它是“是”。

    $(document).ready(function () {
        $('input[type=checkbox]').click(function(){                
            var areYouSure = confirm('Are you sure you want make this change?');
            if (areYouSure) {
                $(this).prop('checked', this.checked);
            } else {
                $(this).prop('checked', !this.checked);
            }
        });
    }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM