【问题标题】:How to replace or alter function WebForm_OnSubmit()如何替换或更改函数 WebForm_OnSubmit()
【发布时间】:2016-07-18 01:13:25
【问题描述】:

我的 Web 表单使用 asp:CustomValidator 验证,因此 ASP.NET 写出 <form method="post" action="./MyPropertiesEnquiry.aspx?rwndrnd=0.30325462348842547" onsubmit="javascript:return WebForm_OnSubmit();" id="form1"> 以及

function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}

只有在验证成功后我才需要向 Google Analytics 发送命中事件,因此我认为我需要在 return true; 之前插入我的代码。

我已经看到了一个几乎解决方案here

但插入的代码对我来说位置错误(它位于 WebForm_OnSubmit() 的开头)。

【问题讨论】:

    标签: javascript c# asp.net webforms


    【解决方案1】:

    您可以在此处简单地添加您的 CustomCode():

    <form id="your_form_id" runat="server" onsubmit="CustomCode();">
    

    之后,您的 WebForm_OnSubmit() 将自动如下所示:

    function WebForm_OnSubmit()
    {
        if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
            return false;CustomCode();
        return true;
    }
    

    【讨论】:

      【解决方案2】:

      我做了一个 Javascript 函数覆盖,因为它似乎是最简单的技术,并且它具有能够精确放置自定义代码的巨大优势。

      WebForm_OnSubmit = function () {
      if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
      
      // Custom code here...
      
      return true;
      }
      

      我在我的网站上对其进行了测试,我能够将数据发送到 Google Analytics(分析),正如我所希望的那样。

      【讨论】:

        猜你喜欢
        • 2018-05-29
        • 2014-04-28
        • 1970-01-01
        • 1970-01-01
        • 2016-09-03
        • 2012-04-30
        • 1970-01-01
        • 2014-10-15
        • 1970-01-01
        相关资源
        最近更新 更多