函数参数未在单击事件内更新

Function parameter not getting updated inside click event

本文关键字:事件 更新 单击 参数 函数      更新时间:2023-09-26

Q:点击事件中的功能参数未更新

**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;
    }
}

基本上,response变量是第一次传递的。您设置了记录响应的单击事件处理程序,并且再也不设置单击处理程序。

该响应变量从未更改-始终使用在原始点击处理程序中设置的响应变量,因为这是您传入的值。相反,您可以尝试将其设置为变量,如:

**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;
    }
}

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