2011-05-12 71 views
0

谁能halpe我这个错误:JavaScript未捕获TypeError?

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); 
    } 

} 
+1

需要一些示例代码 – 2011-05-12 14:29:21

+0

@Sachin Shanbhag更新 – 2011-05-12 14:32:53

+0

您好,当我装你的网站我只是错误信息第895行的“order is null”。您的消息到底会在哪里被解雇? – reporter 2011-05-12 14:35:02

回答

0

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

我得到一个smiliar错误,当我访问您的网页:

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!"); } 
+0

谢谢,我会检查它是否存在 – 2011-05-12 14:41:17

2

检查您的“price”是有效的值。如果“price”未定义,则不能像代码中那样使用行“price.length”。我的猜测是你的price肯定是undefined

+0

您说得对,价格未定,但为什么? – 2011-05-12 14:47:15

+0

@Daniel - 添加一些警报或日志语句来检查Price为何未定义。检查Price_Array和索引中的内容等。缩小问题范围,然后解决问题。 – 2011-05-13 09:39:33

相关问题