2013-01-02 47 views
0

我的.split();方法有問題。.split();使功能運行另一個功能時出錯

我調用這個函數:

get_content_ajax("html/settings.html", "#ajax", 1, "Settings page have been loaded.", "place_settings", "'settings', 'api_username', 'api_password', 'hmm, ja', 'settings'"); 

下面是相關的功能:

function get_content_ajax(load, element, set_status, status, success_function, success_function_values) { 
    element = typeof element !== 'undefined' ? element : "#ajax"; 
    set_status = typeof set_status !== 'undefined' ? set_status : 0; 
    status = typeof status !== 'undefined' ? status : ""; 
    success_function = typeof success_function !== 'undefined' ? success_function : null; 
    success_function_values = typeof success_function_values !== 'undefined' ? success_function_values : ""; 
    $("#work-station").load(load, function(response, status, xhr) { 
     if (status == "success") { 
      if (set_status == 1) { 
       insert_to_element(element, response); 
       stop_loading_img("loader"); 
       start_loading_img("done"); 
       set_loading_status(status); 
       if (success_function != null && success_function != 0) { 
        window[success_function](success_function_values); 
       } 
      } 
     } else { // status = "error" 
      no_connection(); 
     } 
    }); 
} 


function place_settings(id, settings, default_values, page) { 
    var s = settings.split(', '); 
    var d = default_values.split(', '); 
    $.each(s, function(index, value) { 
     //alert(index + ': ' + value + " - default value: " + d[index]); 
     insert_to_element("#" + id + " .", get_setting(value, d[index])); 
    }); 
} 

然後我跑,我得到以下錯誤:

Uncaught TypeError: Cannot call method 'split' of undefined 

我注意到,由於某種原因,變量id具有來自所有變量的所有信息。希望這可以幫助。 OBS:我希望我的代碼有道理,以及我的問題。

謝謝先進。

回答

2

你只傳遞一個undefined值:

if (success_function != null && success_function != 0) { 
        window[success_function](success_function_values); 
} 

所以fi rst參數(id)獲取它。你需要做的是:

function place_settings(data) { 
    //now data contains all the information you need and you can get the values from it 

var id = data['id']; 
... 
    }); 
} 
+1

他還需要調用'get_content_ajax()'作爲最後一個參數的對象。 –

+0

非常感謝@ ftom2,我使用這種方法得到了它的工作。 –

1

我想你沒有通過適當的參數在你的函數調用

place_settings 

這就是爲什麼無論是settings未定義或default_values完全是你

place_settings function.