2015-04-22 2 views
2

Я получаю следующее сообщение об ошибке при нажатии на радиокнопки, который запускает функцию change_type:Uncaught ReferenceError: CustomerType не определен (JQuery)

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. Спасибо заранее!

+2

Вы указали переменную customerType в функции change_customer как частную переменную и получаете доступ к ней в другой функции –

+0

http: // www.w3schools.com/js/js_scope.asp это может быть полезно – Kyborek

+0

W3Schools никогда не бывает полезным. –

ответ

2

Проблема заключается в том, что вы объявляете массив customerType внутри функции change_customer(), поэтому он выходит за рамки функции 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 
} 

Обратите внимание, что в приведенном выше примере включен только соответствующий код.

+0

Без проблем, рад помочь. –

2

Вы определили customerType в функции change_customer, поэтому он недоступен вне этой функции. Чтобы сделать customerType доступным во всех функциях, вам нужно сделать его глобальным.

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

    customerType = temp[value]; 
    console.log(customerType); 
} 
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.

0

хорошо вы не называйте вашу change_customer функции в любом месте внутри функции change_type. Поэтому 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?

0

Функция change_type пытается использовать customerType, если она не была объявлена. Вам необходимо объявить customerType вместе с остальными вашими глобальными переменными, не входящими в область функции change_customer :)

Смежные вопросы