2016-11-23 138 views
1

我有一個腳本來填充數據庫中的數據,但變量我試圖使用變量selected似乎沒有被使用。我的意思是Netbeans告訴我這個變量沒有被使用。腳本有問題嗎?JQuery未使用的變量錯誤

function get_child_options(selected) 
{ 
    if (typeof selected === 'undefined') 
    { 
     var selected = ' '; 
    } 

    var parentID = jQuery('#parent').val(); 

    jQuery.ajax(
    { 
     url: '/MyProjectName/admin/parsers/child_categories.php', 
     type: 'POST', 
     data: 
     { 
      parentID: parentID, 
      selected: selected 
     }, 
     success: function(data) 
     { 
      jQuery('#child').html(data); 
     }, 
     error: function() 
     { 
      alert("Something went wrong with the child options.") 
     }, 
    }); 
} 

jQuery('select[name="parent"]').change(get_child_options); 
+0

'var selected'在塊範圍之外。在'if'(typeof' – bhantol

+0

selected已經在函數params中定義,在vard''之前移除'var''之前,像@dxcorzo在他的回答中顯示的那樣。 – Shanimal

回答

1

刪除您選擇變量。你有一個函數參數和一個名字相同的變量。

function get_child_options(selected) 
{ 
    if(typeof selected === 'undefined') 
    { 
     selected = ' '; 
    } 

    var parentID = jQuery('#parent').val(); 
    jQuery.ajax(
    { 
     url: '/MyProjectName/admin/parsers/child_categories.php', 
     type: 'POST', 
     data: {'parentID': parentID, 'selected': selected}, 
     success: function(data) 
     { 
      jQuery('#child').html(data); 
     }, 
     error: function() 
     { 
      alert("Something went wrong with the child options.") 
     } 
    }); 

    jQuery('select[name="parent"]').change(get_child_options); 
} 
+0

剛試過你的腳本,Netbeans仍然告訴我所選的變量是'未使用'。 –

+0

請再次檢查 –