2016-11-23 125 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

请再次检查 –