我想運行這個確認腳本,但它不適用于 HTML 和 asp.net 按鈕。它只顯示“您點擊了取消”。有人可以幫我嗎?這是我的代碼。
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<button id="btnperfdel" runat=server type="button" class="btn btn-outline-secondary" onclientclick="confirm()"><i class="mdi mdi-delete">Delete</i> </button>
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="confirm()"/>
</div>
<script type = "text/javascript">
function confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Confirm Delete?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
Here's my backend
Protected Sub btnperfdel_Click(ByVal sender As Object, ByVal e As System.EventArgs) 處理 btnperfdel.ServerClick
Dim confirmValue As String = ""
confirmValue = Request.Form("confirm_value")
If confirmValue = "Yes" Then
Label1.Visible = False
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked Cancel')", True)
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim confirmValue As String = ""
confirmValue = Request.Form("confirm_value")
If confirmValue = "Yes" Then
Label1.Visible = False
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked Cancel')", True)
End If
End Sub
uj5u.com熱心網友回復:
您是否使用與內置 js 功能名稱相同的函式名稱確認?
這是一個“非常”糟糕的主意。
嘗試為您的 js 函式名稱指定一個與內置函式不同的名稱。
像這樣說:
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="myconfirm()"/>
<script type = "text/javascript">
function myconfirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Confirm Delete?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
因此,對于函式名稱來說,這看起來是一個非常糟糕的名稱。
另外,請注意,您可以“回傳”真/假按鈕單擊。我認為在 99% 的情況下,用戶點擊取消,然后你不希望代碼運行,不需要進一步的操作。所以你實際上可以這樣做:
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="return myconfirm2()"/>
<script type = "text/javascript">
function myconfirm2() {
return confirm("Confirm Delete?");
}
如果您執行上述操作,則如果用戶單擊取消,則按鈕單擊不會運行。
實際上,您甚至可以轉儲所有代碼,然后執行以下操作:
<asp:Button ID="Button1" runat="server" Text="Button"
onclientclick="return confirm('confirm delete');"/>
因此,如果您為客戶端單擊事件回傳 true 或 false,則服務器端代碼按鈕將不會運行。并且在十分之九的情況下,您不需要知道用戶是否點擊了取消,但只需要知道我們無論如何都不會運行按鈕代碼服務器端。
無論如何,請更改您的名稱確認,因為這是一個內置的 js 函式名稱,使用相同的名稱非常令人困惑。
編輯:================================================ ====
我建議你嘗試/使用這個:
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="myconfirm()"/>
<asp:HiddenField ID="myanswer" runat="server" ClientIDMode="Static" />
<script type = "text/javascript">
function myconfirm() {
var myanswer = document.getElementById("myanswer")
if (confirm("Confirm Delete?")) {
myanswer.value = "Yes"
}
else {
myanswer.value = "No"
}
}
</script>
后面的代碼是:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If myanswer.Value = "Yes" Then
' do whatever here for yes
Debug.Print("yes")
Else
' do whtever here for no
Debug.Print("no")
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/337976.html
上一篇:如何將txt檔案內容添加到文本框
