Javascript未定义函数..但它确实是定义的

时间:2011-12-08 17:57:41

标签: javascript function undefined

我的页面上运行了一些函数。第一个是由select字段中的onclick事件调用的 这个函数然后调用另一个函数zip() 这个函数运行一个小脚本。 如果用户将字段留空,则应通过checkit()函数将该函数作为验证方法调用 它在第二次调用时返回undefined ..来自colorchange()的第一个调用工作..

function colorchange(value){
    if (value != ''){
        document.getElementById('delivery').style.borderColor="#000000";
    }
    else{
        document.getElementById('delivery').style.borderColor="#ff0000";
    }
    if (value == 'deliver'){
    zip();
}
}

function checkit(){

    var x=document.forms["pricing_form"]["delivery"].value;
        if (x==null || x=="")
          {
          alert("Please Choose Delivery Method");
          return false;
          }

        if (document.getElementById('delivery').value == 'deliver'){
            alert ('deliver');
            zipcode = document.getElementById('zip') == '';

            if (zipcode == '' || zipcopde == null || zipcode == 'false'){
                alert('please enter a valid zip code');
                zip();
                return false;
            }
        }
}

function zip(){

    var zip = prompt("Ship-To Zip Code", "");

    if (zip != null && zip != ''){
        document.getElementById('zip').value=zip;
        }
        alert (document.getElementById('zip').value);

}

我按如下方式重命名字段以使此脚本正常运行

function colorchange(value){
    if (value != ''){
        document.getElementById('delivery').style.borderColor="#000000";
    }
    else{
        document.getElementById('delivery').style.borderColor="#ff0000";
    }
    if (value == 'deliver'){
    zip_get();
}
}

function checkit(){

    var x=document.forms["pricing_form"]["delivery"].value;
        if (x==null || x=="")
          {
          alert("Please Choose Delivery Method");
          return false;
          }

        if (document.getElementById('delivery').value == 'deliver'){
            alert ('deliver');
            zipcode = document.getElementById('zip') == '';

            if (zipcode == '' || zipcode == null || zipcode == 'false'){
                alert('please enter a valid zip code');
                zip_get();
                return false;
            }
        }
}

function zip_get(){

    var zip_entry = prompt("Ship-To Zip Code", "");

    if (zip_entry != null && zip != ''){
        document.getElementById('zip').value=zip;
        }
        alert (document.getElementById('zip').value);

}

1 个答案:

答案 0 :(得分:3)

if (zipcode == '' || zipcopde == null || zipcode == 'false'){

您确定方法是undefined还是zipcopde

我也很容易将命名函数命名为变量 - 在JavaScript中比在其他语言中更危险;也许不。应该没关系,但仍然。

相关问题