java-script 未被执行

java-script isn't being executed

本文关键字:执行 java-script      更新时间:2023-09-26

我希望用户在执行服务器端代码之前确认他的操作,但没有任何反应,服务器端代码正在正常执行

这是JS代码:

function confirmation() {
            if (document.getElementById("chkbx").checked==true) {
                return (confirm("want to add as A?"));
            }
            else
                return (confirm("want to add as B?"));                
        }   

当我删除如果(document.getElementById("chkbx").checked==true)它工作正常时!!

我调用这种乐趣。 使用 onClientClick() :

<asp:Button ID="btn" runat="server" OnClick="btn_Click OnClientClick="return confirmation()" />

ASP.NET 用

自己的时髦系统替换服务器控件的ID。

所以当你有

<asp:Button ID="btn" runat="server" OnClick="btn_Click" OnClientClick="return confirmation()" />

它实际上是用随机ID渲染的

<input type="button" id="asp_net09325403854" onclick="btn_Click">

您可以尝试获取呈现的 id 值,例如

function confirmation() {
  if (document.getElementById("<% btn.ClientID %>").checked==true) {
    return (confirm("want to add as A?"));
  }
  else
    return (confirm("want to add as B?"));                
  }   
}