Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 一个提交按钮中有两个onclick函数_Javascript_Onclick_Google Analytics - Fatal编程技术网

Javascript 一个提交按钮中有两个onclick函数

Javascript 一个提交按钮中有两个onclick函数,javascript,onclick,google-analytics,Javascript,Onclick,Google Analytics,我想使用onclick函数将Google Analytics track事件代码添加到submit按钮中,但是那里已经有onclick函数了 现行守则 <input name="Submit" type="submit" class="button" style="float:left; clear:both;" onclick="MM_validateForm('name','','R','contact_number','','R','email','','RisEmail','add

我想使用onclick函数将Google Analytics track事件代码添加到submit按钮中,但是那里已经有onclick函数了

现行守则

<input name="Submit" type="submit" class="button" style="float:left; clear:both;" onclick="MM_validateForm('name','','R','contact_number','','R','email','','RisEmail','address1','','R','address2','','R');return document.MM_returnValue" value="Submit" />

可以这样做吗?

您需要一个回调函数,该函数将在单击事件时触发两个脚本


我只是在猜测,但是。。。如果在返回前粘贴代码会怎么样?

如果使用jQuery,它会工作。在单个元素上可以有多个onclick事件

检查

代码

HTML:

通过上面的链接,您可以执行以下操作:

<input name="Submit" type="submit" class="button" style="float:left; clear:both;" onclick="MM_validateForm('name','','R','contact_number','','R','email','','RisEmail','address1','','R','address2','','R'); _gaq.push(['_trackEvent', 'Contact Page Enquiry', 'Enquiry', 'Contact Page Enquiry']); return document.MM_returnValue" value="Submit" />


顺便说一句,这是未经检验的

需要注意两件事:

  • 您可能只希望在表单通过验证时触发
    \u trackEvent
    调用
  • 事件跟踪通过从分析服务器请求跟踪像素来工作。如果表单提交导致在跟踪请求完成之前将新页面加载到同一窗口,则最终可能会丢失或部分收集数据
  • 您可以通过延迟表单提交来绕过第二个问题

    <input id="Submit" name="Submit" type="submit" ... />
    ...
    $('#Submit').click(function(e) {
      // Prevent the default handling of the form submit button
      e.preventDefault();
      MM_validateForm('name', ...
      // If the form didn't validate, don't do anything else
      if (!document.MM_returnValue) return;
      _gaq.push(['_trackEvent', ...
      // Get the form & submit it after 150ms delay
      var form = $(this).parents('form:first');
      setTimeout(function() {form.submit()},150);
    });
    
    
    ...
    $(“#提交”)。单击(函数(e){
    //阻止表单提交按钮的默认处理
    e、 预防默认值();
    MM_validateForm('name'。。。
    //如果表单未验证,请不要执行其他操作
    如果(!document.MM_returnValue)返回;
    _gaq.push([''u trackEvent'。。。
    //获取表格并在150毫秒延迟后提交
    var form=$(this.parents('form:first');
    setTimeout(函数(){form.submit()},150);
    });
    
    谢谢,但我确实试过了,GA没有记录这一事件。
    <a id="a" onclick="alert(1)">click</a>​
    
    <input name="Submit" type="submit" class="button" style="float:left; clear:both;" onclick="MM_validateForm('name','','R','contact_number','','R','email','','RisEmail','address1','','R','address2','','R'); _gaq.push(['_trackEvent', 'Contact Page Enquiry', 'Enquiry', 'Contact Page Enquiry']); return document.MM_returnValue" value="Submit" />
    
    <input id="Submit" name="Submit" type="submit" ... />
    ...
    $('#Submit').click(function(e) {
      // Prevent the default handling of the form submit button
      e.preventDefault();
      MM_validateForm('name', ...
      // If the form didn't validate, don't do anything else
      if (!document.MM_returnValue) return;
      _gaq.push(['_trackEvent', ...
      // Get the form & submit it after 150ms delay
      var form = $(this).parents('form:first');
      setTimeout(function() {form.submit()},150);
    });