【问题标题】:Uncaught ReferenceError: customerType is not defined (jQuery)未捕获的 ReferenceError:customerType 未定义(jQuery)
【发布时间】:2015-04-22 09:38:35
【问题描述】:

单击触发 change_type 函数的单选按钮时出现以下错误:

Uncaught ReferenceError: customerType is not defined

我正在使用以下代码:

var date_selected = false;
var selected_year = false;
var current_customer = false;
var current_type = false;
var previous_value = 0;

var temp = <?php print_r($json); ?>;
var part = temp['Particulier'];
//console.log(part);
var type = part['Weekend'];
//console.log(type);
var yearArray = type['2016'];
//console.log(yearArray);

var dateSelect = document.getElementsByClassName('dateSelect');
var yearLabel = $("label[for='year']");
var year = document.getElementById('year');
var month = document.getElementById('month');
var date = document.getElementById('date');

$(dateSelect).hide();
yearLabel.hide();

function change_customer(value) {
    current_customer = value;

    var customerType = temp[value];
    console.log(customerType);
}

function change_type(value) {
    $(dateSelect).show();
    yearLabel.show();
    current_type = value;
    if(previous_value == 0) {
        $(dateSelect).show();
    }

    if(current_type == 'Weekend') {
        year.selectedIndex = 0;
        month.style.visibility = 'hidden';
        date.style.visibility = 'hidden';
    } else if(current_type == 'Midweek') {
        year.selectedIndex = 0;
        month.style.visibility = 'hidden';
        date.style.visibility = 'hidden';
    } else if(current_type == 'Week') {
        year.selectedIndex = 0;
        month.style.visibility = 'hidden';
        date.style.visibility = 'hidden';
    }
    previous_value = value;

    var dateType = customerType[value];
    console.log(dateType);
}

如果需要任何其他信息,请询问,新手,以及 jQuery。 提前致谢!

【问题讨论】:

  • 您已在 change_customer 函数中将 customerType 变量声明为私有变量,并在其他函数中访问它
  • W3Schools 从来没有用处。

标签: jquery arrays json


【解决方案1】:

问题是因为您在change_customer() 函数内声明了customerType 数组,所以它超出了change_type() 函数的范围。您需要在两者的范围内声明它。试试这个:

var customerType = []; // declare empty to prevent unexpected 'access by index' errors

function change_customer(value) {
    customerType = temp[value]; // remove the 'var'
}

function change_type(value) {
    var dateType = customerType[value]; // is now in scope
}

请注意,我在上面的示例中只包含了相关代码。

【讨论】:

  • 没问题,很乐意提供帮助。
【解决方案2】:

您在函数change_customer 中定义了customerType,因此在该函数之外无法访问它。要使 customerType 在所有函数中都可访问,您需要将其设为全局。

var customerType = []; // In global scope(outside of all functions)
.
.
.
function change_customer(value) {
    current_customer = value;

    customerType = temp[value];
    console.log(customerType);
}

【讨论】:

    【解决方案3】:
    function change_customer(value) {
        current_customer = value;
    
        var customerType = temp[value];
        console.log(customerType);
    }
    
    function change_type(value) {
        $(dateSelect).show();
        yearLabel.show();
        current_type = value;
        if(previous_value == 0) {
            $(dateSelect).show();
        }
    
        if(current_type == 'Weekend') {
            year.selectedIndex = 0;
            month.style.visibility = 'hidden';
            date.style.visibility = 'hidden';
        } else if(current_type == 'Midweek') {
            year.selectedIndex = 0;
            month.style.visibility = 'hidden';
            date.style.visibility = 'hidden';
        } else if(current_type == 'Week') {
            year.selectedIndex = 0;
            month.style.visibility = 'hidden';
            date.style.visibility = 'hidden';
        }
        previous_value = value;
    
        var dateType = customerType[value];
        console.log(dateType);
    }
    

    在上面的代码中你已经在里面初始化了customerType变量 change_customer 函数,而您正试图在超出其范围的 change_type 函数中访问它 范围,所以你的选择是让它像你做一些变量一样全球化。

    【讨论】:

      【解决方案4】:

      你不要在change_type 函数内的任何地方调用你的change_customer 函数。因此customerType 永远不会被实例化。

      尝试像这样添加你的函数:

      function change_customer(value) {
          var current_customer = value;
      
          var customerType = temp[value];
          return customerType;
      }
      
      function change_type(value) {
          var customerType = change_customer(value);
          $(dateSelect).show();
          yearLabel.show();
          ......
      }
      

      虽然,您的 temp 数组在哪里,键为 value

      【讨论】:

        【解决方案5】:

        您的 change_type 函数在尚未声明时尝试使用 customerType。您需要声明 customerType 以及其余的全局变量,而不是在 change_customer 函数的范围内:)

        【讨论】:

          猜你喜欢
          • 2013-06-29
          • 1970-01-01
          • 2019-01-23
          • 2014-04-12
          • 2016-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多