JavaScript Uncaught TypeError?

时间:2011-05-12 14:27:55

标签: javascript jquery

任何人都可以帮我解决这个错误:

Uncaught TypeError: Cannot read
property 'length' of undefined

website

当你从组合中更改值时,它应该更改下面的价格,但它不起作用

function update_price_select2(o, element_name)
{   

    var sel_obj = null;
    var element = null;
    //price_original = <?php //echo substr($products_price,0,-1); ?>;
    //alert("Preço Original:" + price_original);
    if (document.getElementById)  // DOM3 = IE5, NS6
    {  
        sel_obj = document.getElementById(o);
         //alert("sel_obj "+ sel_obj);
        element = document.getElementById(element_name);
         //alert("element "+ element);
    }
    //alert(o);
    //alert(element_name);
    var index = sel_obj.selectedIndex;
    //alert(o+" index");
    var price_array = option_price[sel_obj.id];
    var price = price_array[index];
    var price_for_operation = price.substr(0,price.length -1); //the price from the option choosen

    if (price_for_operation == '')
    {
        price_for_operation = 0;
    }

    //Ao preço original, vamos retirar o preço da variável que este tinha anteriormente.
    current_product_price = current_product_price - option_price[sel_obj.id + '_current'];
    //Vamos colocar o valor actual da variável seleccionada 
    option_price[sel_obj.id + '_current'] = price_for_operation;

    if (price_for_operation != 0)
    {
        var final_price = roundNumber(parseFloat(current_product_price) + parseFloat(price_for_operation),2); 
        final_price = final_price.toFixed(2);
    }
    else
    {
        var final_price = current_product_price.toFixed(2);
    }
    //Nos save the decimal value, without the € or $, so we can use it in the next options call
    current_product_price = final_price;
    final_price = final_price + money_simbol;  

    if (final_price != "")
    {    
        display_updated_price(final_price, element);
        update_allowbuy(final_price);
    }

}

2 个答案:

答案 0 :(得分:2)

检查“price”是否为有效值。如果未定义“price”,则无法在代码中使用“price.length”行。我的猜测是price肯定是undefined

答案 1 :(得分:0)

你可以发布错误发生的行吗?您的错误告诉我您尝试访问无法找到的对象的属性长度。

访问您的网页时出现了一个熟悉的错误:

var order = document.getElementById('".BUTTON_IN_CART."'); 
order.style.visibility = 'visible';` 

告诉我“订单”未定义,这是因为它无法找到变量BUTTON_IN_CART,因此它不会获得元素!您应该始终检查您要查找的元素是否存在。

编辑:

扩展代码以检查是否找到了元素:

if (document.getElementById)  // DOM3 = IE5, NS6
{  
    sel_obj = document.getElementById(o);
     //alert("sel_obj "+ sel_obj);
    element = document.getElementById(element_name);
     //alert("element "+ element);
}

if(sel_obj == null) { alert("Sell Obj not found!"); }
if(element == null) { alert("Element not found!"); }