简体   繁体   English

从元素中删除addListener(来自Google Analytics(分析)的事件)

[英]Remove addListener (Event from Google Analytics) from element

I am launching an event to google analytics every time the user chooses a different select option. 每当用户选择其他select选项时,我都会向Google Analytics(分析)发起一个事件。 But when the user clicks again on the select event is fired again. 但是当用户再次单击select事件时,将再次触发。 I'm trying to remove the element event after the user chooses the option. 我试图在用户选择选项后删除element事件。 I've tried unbind() and off() but is not working. 我试过unbind()off()但是没有用。 Can anyone help me please? 谁能帮我吗?

JS: JS:

$('#estado').change(function(){
     var getEstado = $('#estado option:selected').val();
     var getEstadoDesc = $('#estado option:selected').text();

     var homeEstado = document.getElementById('estado');
     addListener(homeEstado, 'click', function() {
         ga('send', 'event', 'Busca', 'Estado', getEstadoDesc);
         removeEventHandler(homeEstado, 'click');
     });

     function addListener(element, type, callback) {
        if (element.addEventListener) element.addEventListener(type, callback);
        else if (element.attachEvent) element.attachEvent('on' + type, callback);
     }

     function removeEventHandler(element, type, callback) {
         if (element.removeEventListener) element.removeEventListener(type,callback);
         else if (element.detachEvent) element.detachEvent('on' + type, callback); 
     }

     $.ajax({  
         type: 'POST',
         data: {estado: getEstado},
         url: './ajax/get_cidade.php',
         beforeSend: function(data){

         },
         success: function(data){
             $('#load-cursos').html(data);
         },
     });
 });

I just make a alter to your code, please see if that is what you want. 我只是对您的代码进行了更改,请查看是否正是您想要的。

 var selecEvent = function(){ var getEstado = $('#estado option:selected').val(); var getEstadoDesc = $('#estado option:selected').text(); function addListener(element, type, callback) { if (element.addEventListener) element.addEventListener(type, callback); else if (element.attachEvent) element.attachEvent('on' + type, callback); } function removeListener(element, type, callback) { if (element.addEventListener) element.removeEventListener(type, callback); else if (element.attachEvent) element.detachEvent('on' + type, callback); } var homeEstado = document.getElementById('estado'); var sendFunction = function() { alert('Selected ' + getEstadoDesc + ' with value ' + getEstado); removeListener(homeEstado, 'click', sendFunction); }; addListener(homeEstado, 'click', sendFunction); // Stop listen to any changes to prevent multiple ajax call and alerts... etc. $('#estado').off(); setTimeout(function() { alert('Select changed'); // Now every thing done. Event sent, content load, we can listen to select again. $('#estado').change(selecEvent); }, 3000); } $('#estado').change(selecEvent); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="estado"> <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> <option value="d">D</option> </select> 

removeListener or detachEvent, form document, needs a function to remove, so I have to alter the execute ga to a simple alert function. removeListener或detachEvent,表单文档,需要一个函数来删除,因此我必须将execute ga更改为一个简单的警报函数。 And altered ajax to a settimeout to simulate ajax delay. 并将ajax更改为settimeout以模拟ajax延迟。

Take to look to see if I misunderstood something? 看一下我是否误会了什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM