简体   繁体   English

函数参数未在点击事件中更新

[英]Function parameter not getting updated inside click event

Q: Function parameter not getting updated inside click event 问:点击事件中的功能参数未更新

**Event.js**

// main click event to call
$(document).on('click', '.start', function(){
    root.ajax({
        url: 'location'
    }, function( response ){
        root.update( response );
    })
});

**Content.js**

var flag = false;
root.update = function( response ){ 
    if(!flag){
        // event assignment for new created button
        $(document).on('click', '.innerStart', function(){
            // first time prints okay but after printing old value always
            // response is not getting updated
            console.log( response );
        });
        flag = true;
    }
}

Basically, the response variable is passed in the first time. 基本上, response变量是第一次传递的。 You set the click event handler, which logs the response, and you never set the click handler again. 您设置单击事件处理程序,该事件处理程序将记录响应,并且您再也不会设置单击处理程序。

That response variable is never changed - the one that was set in the original click handler is always used, because that's the value you passed in. Instead, you could try set it as a variable, like: 该响应变量永远不会更改-始终使用在原始点击处理程序中设置的响应变量,因为这是您传入的值。相反,您可以尝试将其设置为变量,例如:

**Event.js**
var response;
// main click event to call
$(document).on('click', '.start', function(){
    root.ajax({
        url: 'location'
    }, function( responseValue ){

        root.update( responseValue );
    })
});

**Content.js**

var flag = false;
root.update = function( responseValue ){    
    response = responseValue;
    if(!flag){
        // event assignment for new created button
        $(document).on('click', '.innerStart', function(){
            // first time prints okay but after printing old value always
            // response is not getting updated
            console.log( response );
        });
        flag = true;
    }
}

看起来flag变量设置为true,这使更新运行一次。

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

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