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

时间:2014-10-17 07:59:00

标签: javascript jquery javascript-events

问:功能参数未在点击事件中更新

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

2 个答案:

答案 0 :(得分:1)

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

该响应变量永远不会更改 - 始终使用原始单击处理程序中设置的响应变量,因为它是您传入的值。相反,您可以尝试将其设置为变量,如:< / p>

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

答案 1 :(得分:0)

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