尽管变量是全局声明的,但变量未定义

时间:2015-02-19 03:50:10

标签: javascript

function begin(firstName){
firstName = document.getElementById('name').value;
var age = document.getElementById('age').value;
console.log(firstName);
if (firstName == ""){
    alert("please enter a name here");
    return;
}
if (age == ""){
    alert("please enter your age here");
    return;
}
if(0 < age && age < 9){
    location.href = "toys.html";
    return;
}
if(10 < age && age < 16){
    location.href = "phones.html";
    return;
}
else{
    location.href = "cars.html";
}};
`table = document.getElementsByTagName('table')[0].childNodes[1];
row = table.childNodes[2];
price =[table.childNodes[2].childNodes[3].innerHTML.substr(1),table.childNodes[4].childNodes[3].innerHTML.substr(1), table.childNodes[6].childNodes[3].innerHTML.substr(1)];
amount = [table.childNodes[2].childNodes[7].innerHTML.substr(1), table.childNodes[4].childNodes[7].innerHTML.substr(1), table.childNodes[6].childNodes[7].innerHTML.substr(1)];`
function update(){
console.log(firstName);
q1 = parseInt(document.getElementById('quantity1').value);
q2 = parseInt(document.getElementById('quantity2').value);
q3 = parseInt(document.getElementById('quantity3').value);
if(isNaN(q3) == true){
    q3 = 0;
}
if(isNaN(q2) == true){
    q2 = 0;
}
if(isNaN(q1) == true){
    q1 = 0;
}
x = q1 + q2 + q3;
var amount1 = document.getElementById('amount1').innerHTML = "$" + price[0] * q1;
    amount2 = document.getElementById('amount2').innerHTML = "$" + price[1] * q2;
    amount3 = document.getElementById('amount3').innerHTML = "$" + price[2] * q3;
    n = parseInt(price[0]) * q1 + parseInt(price[1]) * q2  + parseInt(price[2]) * q3;
document.getElementById('totalAmount').innerHTML = "$" + n;
table.childNodes[8].childNodes[5].innerHTML = x;}

当脚本移动到下一页时,变量firstName继续以未定义的形式出现,我想我可以在另一个函数中使用该变量,因为它是全局声明的,但由于某种原因,它会一直未定义。

0 个答案:

没有答案