简体   繁体   English

点击事件处理程序数据中的jQuery在AJAX处理程序中为“未定义”

[英]jQuery on click event handler data is 'undefined' within AJAX handler

I have a jQuery event handler function that is triggered on click of two buttons - Positive and Negative. 我有一个jQuery事件处理程序函数,可通过单击两个按钮(正数和负数)来触发。 In either case, I pass a different data object to the handler (as below). 无论哪种情况,我都将不同的数据对象传递给处理程序(如下所示)。

I have a AJAX POST within this event handler. 我在此事件处理程序中有一个AJAX POST。 However I am unable to access the on click event's data inside the AJAX POST handler. 但是,我无法访问AJAX POST处理程序中的click事件的数据。

$('#Positive').on("click", {agree:true}, handlerFunc);
$('#Negative').on("click", {agree:false}, handlerFunc);

function handlerFunc(e){
    e.preventDefault();
   //e.data is valid here
   if (e.data.agree === true){
        //if condition is valid and it enters this block
        startTimer();
    }
   $.post(url_, postData, function(data){ 
        //console.log(e.data); 
        //e.data is undefined at this point
        if (e.data.agree === false){
             return true; 
        }
        //do other stuff
    });
}

Why is e.data undefined within the AJAX POST? 为什么在AJAX POST中未定义e.data?

I created a new variable to store the e.data.agree value and it works as expected. 我创建了一个新变量来存储e.data.agree值,它可以按预期工作。

$('#Positive').on("click", {agree:true}, handlerFunc);
$('#Negative').on("click", {agree:false}, handlerFunc);

function handlerFunc(e){
    e.preventDefault();
   //e.data is valid here
   var agree_data = e.data.agree;
   if (e.data.agree === true){
        //if condition is valid and it enters this block
        startTimer();
    }
   $.post(url_, postData, function(data){ 
        //console.log(agree_data); 
        //agree_data is valid at this point
        if (agree_data === false){
             return true; 
        }
        //do other stuff
    });
}

Why is e.data undefined in the first case - within the AJAX POST? 为什么在第一种情况下-在AJAX POST中未定义e.data? Does it get overwritten? 它会被覆盖吗?

NOTE: I have two different environments and it seems to work fine in the test environment but gives the undefined error in production. 注意: 我有两个不同的环境,它在测试环境中似乎可以正常工作,但在生产中会出现未定义的错误。

Summary: 摘要:

From what I understand you're seeing that when you directly call the event object (e) in your onclick handler, you intermittently get an error telling you that 'e.data.agree' is undefined. 据我了解,您看到的是,当您在onclick处理程序中直接调用事件对象(e)时,您会间歇性地收到一条错误消息,告诉您'e.data.agree'未定义。

I actually had to do a bit of reading to be able to explain this better, but as I understand it, it's because the event object that is passed into your click handler is pooled and is later reused for any new events. 我实际上需要做一些阅读才能更好地解释这一点,但是据我了解,这是因为传递到您的点击处理程序中的事件对象已合并,以后可用于任何新事件。

Here is an explanation of SyntheticEvent: Mayank Shukla's SyntheticEvent Explaination 这是对SyntheticEvent的解释: Mayank Shukla的SyntheticEvent解释

Why This Matters To Your Code: 为什么这对您的代码很重要:

In your code you post to a URL, which obviously have variable latency to return a response and hence fire your post's callback function. 在您的代码中,您发布到URL,该URL显然具有可变的延迟以返回响应,从而触发您帖子的回调函数。 In your test environment I'm betting the response is fast enough that the event hasn't been overwritten or nullified. 在您的测试环境中,我敢打赌,响应速度足够快,因此事件没有被覆盖或无效。 However, in your deployed version there is enough latency that the event has been overwritten. 但是,在您部署的版本中,有足够的延迟,该事件已被覆盖。

While it's not exactly true, you can think of the event (e) object as a global variable instead of a local one. 尽管并非完全正确,但您可以将事件(e)对象视为全局变量,而不是局部变量。

The Solution: 解决方案:

You've already solved the problem with your second snippet. 您已经用第二个片段解决了该问题。 By assigning the event object to a local variable, you ensure that the event object is what you are expecting when your post request response fires the callback function. 通过将事件对象分配给局部变量,可以确保事件对象是发布请求响应触发回调函数时所期望的对象。

I hope that explanation makes sense. 我希望这种解释是有意义的。

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

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