2017-01-18 23 views
-2

是否可以傳遞值爲success函數?傳遞值爲成功函數

function updateData(data_type){ 
    var current_url = "myUrl"; 
    var opc = data_type; 
    var dataset = []; 
    $.ajax({ 
    data : {'type': opc}, 
    url : current_url, 
    type : 'get', 
    success: function(data){ 
     update(data) 
    } 
    }); 
    console.log("update: " + data_type); 
} 

裏面的success功能我要訪問我的data_type變量,我路過參數。

這可能嗎?

+1

是。你可以直接訪問'data_type'。 JavaScript具有詞彙範圍。也許你可以詳細說明你嘗試過什麼,以及你有什麼問題。 –

+0

'data_type'看起來像已經在範圍內。你有沒有嘗試過在你的成功功能中使用它? –

回答

1

直接使用它,就像這樣:

function updateData(data_type){ 
    var current_url = "myUrl"; 
    var opc = data_type; 
    var dataset = []; 
    $.ajax({ 
    data : {'type': opc}, 
    url : current_url, 
    type : 'get', 
    success: function(data){ 
     update(data, data_type); 
    } 
    }); 
    console.log("update: " + data_type); 
} 
+0

謝謝@ Tha'er Al-Ajlouni!我不知道發生了什麼之前,我檢查功能,我可以使用的值:( – Stone

+0

歡迎您,請將它標記爲未來參考的正確答案。 –

1
function updateData(data_type) { 

    var current_url = "myUrl"; 
    var opc = data_type; 
    var dataset = []; 

    $.ajax({ 
    data : {'type': opc}, 
    url : current_url, 
    type : 'get', 
    success: function(data){ 
     // `data_type` is available here, use as needed. 
     // You can pass it to other functions inside this one as well, 
     // i.e. the function below could be `update(data, data_type);`. 
     update(data); 
    } 
    }); 

    console.log("update: " + data_type); 

} 
+0

謝謝@ hungerstar !!我不知道之前發生了什麼我檢查功能,我可以使用值:( – Stone

相關問題