未捕获的ReferenceError:没有定义customerType (jQuery)

时间:2021-11-27 08:59:45

I am getting the following error when clicking on the radiobutton that triggers the change_type function:

点击触发change_type函数的radiobutton时,我得到如下错误:

Uncaught ReferenceError: customerType is not defined

未捕获的ReferenceError: customerType没有定义

I am using the following code:

我使用的代码如下:

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

If any other information is required please ask, new to this, as well as jQuery. Thanks in advance!

如果需要任何其他信息,请询问,这是新的,还有jQuery。提前谢谢!

5 个解决方案

#1


2  

The issue is because you declare the customerType array inside the change_customer() function, so it is out of scope of the change_type() function. You need to declare it within scope of both of them. Try this:

问题在于,您在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
}

Note that I only included the relevant code in the above example.

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

#2


2  

You have defined customerType in function change_customer, so it is not accessible outside of that function. To make the customerType accessible in all the functions, you'll need to make it global.

您已经在函数change_customer中定义了customerType,因此在该函数之外无法访问它。要使customerthe类型在所有函数中都可访问,您需要使它成为全局的。

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

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

#3


0  

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

In the above code you have initialized customerType variable inside the change_customer function and you are trying to access it in change_type function which is beyond its scope scope so your choice is make it globalized as u did some variables.

在上面的代码中,您已经在change_customer函数中初始化了customerType变量,并且您正在尝试在change_type函数中访问它,该函数超出了它的作用域范围,因此您的选择是使它全球化,就像您所做的一些变量一样。

#4


0  

well you do not call your change_customer function anywhere inside the change_type function. Therefore customerType will never be instatiated.

你不会在change_type函数中调用change_customer函数。因此,customerType永远不会被恢复。

try adding your function like this:

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

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

although, where is your temp array with the key value?

但是,带键值的临时数组在哪里?

#5


0  

Your change_type function is trying to use customerType when it has not been declared. You need to declare customerType along with the rest of your global variables, not within the scope of the change_customer function :)

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

#1


2  

The issue is because you declare the customerType array inside the change_customer() function, so it is out of scope of the change_type() function. You need to declare it within scope of both of them. Try this:

问题在于,您在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
}

Note that I only included the relevant code in the above example.

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

#2


2  

You have defined customerType in function change_customer, so it is not accessible outside of that function. To make the customerType accessible in all the functions, you'll need to make it global.

您已经在函数change_customer中定义了customerType,因此在该函数之外无法访问它。要使customerthe类型在所有函数中都可访问,您需要使它成为全局的。

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

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

#3


0  

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

In the above code you have initialized customerType variable inside the change_customer function and you are trying to access it in change_type function which is beyond its scope scope so your choice is make it globalized as u did some variables.

在上面的代码中,您已经在change_customer函数中初始化了customerType变量,并且您正在尝试在change_type函数中访问它,该函数超出了它的作用域范围,因此您的选择是使它全球化,就像您所做的一些变量一样。

#4


0  

well you do not call your change_customer function anywhere inside the change_type function. Therefore customerType will never be instatiated.

你不会在change_type函数中调用change_customer函数。因此,customerType永远不会被恢复。

try adding your function like this:

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

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

although, where is your temp array with the key value?

但是,带键值的临时数组在哪里?

#5


0  

Your change_type function is trying to use customerType when it has not been declared. You need to declare customerType along with the rest of your global variables, not within the scope of the change_customer function :)

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