【问题标题】:JQuery Unused Variable ErrorJQuery未使用的变量错误
【发布时间】:2016-11-23 22:08:16
【问题描述】:

我有一个脚本可以从数据库中填充数据,但我尝试使用的变量 selected 似乎没有被使用。我的意思是 Netbeans 告诉我该变量未使用。脚本有问题吗?

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);

【问题讨论】:

  • var selected 在块范围之外。将var selected 移动到if(typeof 之前
  • selected 已在函数参数中定义。在@dxcorzo 在他的回答中显示之前删除var

标签: jquery unused-variables


【解决方案1】:

删除您的 selected 变量。你有一个函数参数和一个同名的变量。

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);
}

【讨论】:

  • 刚试过你的脚本,Netbeans 仍然告诉我选择的变量是unused
  • 请再检查一遍
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
  • 2011-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-05
相关资源
最近更新 更多