2012-11-01 30 views
0

我的問題是爲什麼當我點擊按鈕時,顯示的唯一東西是這個「價格表」,其他變量中的所有行都被忽略。在onClick事件後顯示的變量遇到問題

function showmessage() { 
    var sendcost, totalcost; 
    if (document.pricelist.total.value<11) { 
     sendcost = 3; 
    } 
    else { 
     sendcost = 2; 
    } 
    if (document.pricelist.option.checked) { 
     sendcost = parseInt(document.pricelist.option.value) + parseInt(sendcost); 
    } 
    totalcost = parseInt(sendcost) + parseInt(document.pricelist.total.value); 

    document.write("Pricelist","</br>"); 
    document.write("Products price: "+document.pricelist.total.value+"</br>"); 
    document.write("Send fee: "+sendcost+"</br>"); 
    document.write("Total cost: "+totalcost+"</br>"); 
} 
<form name="pricelist"> 
Tuna Salad = 4 <input type="checkbox" name="choice" value="4" onchange="checkTotal()" /></br> 
Pasta = 13 <input type="checkbox" name="choice" value="13" onchange="checkTotal()" /></br> 
Milk = 3 <input type="checkbox" name="choice" value="3" onchange="checkTotal()" /></br> 
Chocolate = 2 <input type="checkbox" name="choice" value="2" onchange="checkTotal()" /></br> 
Same day delivery<input type="checkbox" name="option" value="5" /></br> 
Total: <input type="text" size="2" name="total" value="0"/> 
<input type="button" value="Procceed to checkout" onclick='return showmessage();' /> 
+1

你在你的代碼的語法錯誤。注意語法高亮。 – Musa

+0


不是
BumbleB2na

回答

0

document.write當頁面加載後調用會破壞整個頁面,因此不會有任何形式的名稱價目表再因此document.pricelist.total.value會造成Uncaught TypeError: Cannot read property 'total' of undefined,這會導致你的腳本終止過早。

我建議使用非document.write以外的東西。

但爲了避免當前的錯誤只是document.pricelist.total.value值保存到變量其銷燬。

var value = document.pricelist.total.value; 
document.write("Pricelist","</br>"); 
document.write("Products price: "+value+"</br>"); 
document.write("Send fee: "+sendcost+"</br>"); 
document.write("Total cost: "+totalcost+"</br>"); 
+0

我明白了。我該如何解決這個問題? – fotis179

+0

@ fotis179查看更新 – Musa

+0

GOD BLESS YOU !!非常感謝! – fotis179

0

停止顯示後工作的原因,「產品價格:」是因爲接下來的兩行代碼的語法錯誤。

更新:

document.write("Send fee: "+sendcost+"</br>); 
document.write("Total cost: "+totalcost+"</br>; 

到:

document.write("Send fee: "+sendcost+"</br>"); 
document.write("Total cost: "+totalcost+"</br>"); 

而且更新:

document.write("Pricelist","</br>"); 

到:

document.write("Pricelist</br>"); 

,然後修復您的畸形</br>標籤是<br />

+0

是的我犯了一個錯誤,並將其編輯到正確的一個,但它仍然只顯示價格表。 – fotis179

+0

添加了一些更新。希望它有效。 – BumbleB2na

0

使用Firebug進行調試。真正優秀的火狐工具 http://getfirebug.com/

Chrome和Internet Explorer也有一個相當不錯的內部調試工具 按F12打開它。

這些工具讓你直觀地進行調試 - 您可以通過每個腳本命令退一步,看看會發生什麼