该对象没有方法拆分

时间:2013-09-10 10:22:30

标签: jquery

HEllo家伙我有一个在Chrome和FF中运行良好的脚本但似乎无法在IE中运行... 虽然它有一个java脚本错误,我无法解决它 错误是

  

Uncaught TypeError:Object function(){var b = this; var d = []; for(var   一个= b.length个;一​​ - ;){风险   c = b [a]; if(jQuery.inArray(c,d)=== - 1)d.unshift(c)} return d}没有   方法'拆分'

请帮忙

    function populate(s1,s2){
    var s1 = document.getElementById(s1);
    var s2 = document.getElementById(s2);
    s2.innerHTML = "";
    if(s1.value == "bedroom"){
        var optionArray = ["|Room Type","private|Private","shared|Shared","suite|Suite"];
    } else if(s1.value == "house"){
        var optionArray = ["|House Type","beach_house|Beach House","bungalow|Bungalow","cabin|Cabin","chateau|Chateau","house|House","cottage|Cottage","lake_house|Lake House","ski_chalet|Ski Chalet","townhosue|Townhouse","villa|Villa"];
    } else if(s1.value == "apartment"){
        var optionArray = ["|Appartment Type","apartment|Apartment","apartment_hotel|Apartment Hotel","condo|Condo","loft|Loft","luxury_apartment|Luxury Apartment","serviced_aparment|Serviced Apartment"];
    } else if(s1.value == "bnb"){
        var optionArray = ["|B&B Type","room|Room","suite|Suite"];
    } else if(s1.value == "hotel"){
        var optionArray = ["|Hotel Type","boutique_hotel|Boutique Hotel","budget_hotel|Budget Hotel","hotel|Hotel","inn|Inn","resort|Resort"];
    } else if(s1.value == "hostel"){
        var optionArray = ["|Hostel Type","hostel|Hostel"];
    }
    for(var option in optionArray){
        var pair = optionArray[option].split("|");
        var newOption = document.createElement("option");
        newOption.value = pair[0];
        newOption.innerHTML = pair[1];
        s2.options.add(newOption);
    }
}

2 个答案:

答案 0 :(得分:0)

看起来您正在使用一些扩展来为数组原型添加属性,因此IE会运行其他浏览器不具备的其他值。直接修复是not use for..in on your array,而是使用普通for代替:

for(var i = 0; i < optionArray.length; ++i) {
    // and now use optionArray[i] instead of optionArray[option]
}

另一种选择是用

过滤掉这些属性
for(var option in optionArray){
    if (!optionArray.hasOwnProperty(option)) continue;
    // the rest without modification
}

答案 1 :(得分:0)

您在if和其他内容中定义var optionArray if,但不在通用else中,因此在某些情况下optionArray可能未定义。

您可以在if的顶部移动定义,或者定义一般的else。

代码:

function populate(s1,s2){
    var s1 = document.getElementById(s1);
    var s2 = document.getElementById(s2);
    s2.innerHTML = "";
    var optionArray=new Array();
    if(s1.value == "bedroom"){
        optionArray = ["|Room Type","private|Private","shared|Shared","suite|Suite"];
    } else if(s1.value == "house"){
        optionArray = ["|House Type","beach_house|Beach House","bungalow|Bungalow","cabin|Cabin","chateau|Chateau","house|House","cottage|Cottage","lake_house|Lake House","ski_chalet|Ski Chalet","townhosue|Townhouse","villa|Villa"];
    } else if(s1.value == "apartment"){
        optionArray = ["|Appartment Type","apartment|Apartment","apartment_hotel|Apartment Hotel","condo|Condo","loft|Loft","luxury_apartment|Luxury Apartment","serviced_aparment|Serviced Apartment"];
    } else if(s1.value == "bnb"){
        optionArray = ["|B&B Type","room|Room","suite|Suite"];
    } else if(s1.value == "hotel"){
        optionArray = ["|Hotel Type","boutique_hotel|Boutique Hotel","budget_hotel|Budget Hotel","hotel|Hotel","inn|Inn","resort|Resort"];
    } else if(s1.value == "hostel"){
        optionArray = ["|Hostel Type","hostel|Hostel"];
    }
    for(var option in optionArray){
        var pair = optionArray[option].split("|");
        var newOption = document.createElement("option");
        newOption.value = pair[0];
        newOption.innerHTML = pair[1];
        s2.options.add(newOption);
    }
}