簡體   English   中英

函數參數未在點擊事件中更新

[英]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;
    }
}

基本上, 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;
    }
}

看起來flag變量設置為true,這使更新運行一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM