我需要能够创建一个单独的函数,该函数从 2 个不同函数中的变量创建一个总数字。
function addToy1()
{
var i = document.getElementById("qty_toy1");
var qtytoy1 = i.options[i.selectedIndex].text;
var qtytoy1tot = qtytoy1 * 26.99;
var generateTableToy1="<table width=\"210px\" border=\"0\">"
+" <tr>"
+" <td width=\"140\">Optimus Prime</td>"
+" <td width=\"25\">x " + qtytoy1 + "</td>"
+" <td width=\"45\">£" + qtytoy1tot + "</td>"
+" </tr>"
+"</table>";
document.getElementById("toy1_add").innerHTML=generateTableToy1;
}
function addToy2()
{
var i = document.getElementById("qty_toy2");
var qtytoy2 = i.options[i.selectedIndex].text;
var qtytoy2tot = qtytoy2 * 14.39;
var generateTableToy2="<table width=\"210px\" border=\"0\">"
+" <tr>"
+" <td width=\"140\">Bumblebee</td>"
+" <td width=\"25\">x " + qtytoy2 + "</td>"
+" <td width=\"45\">£" + qtytoy2tot + "</td>"
+" </tr>"
+"</table>";
document.getElementById("toy2_add").innerHTML=generateTableToy2;
}
使用此代码,我需要将两个变量“qtytoy1tot”和“qtytoy2tot”相加。我是 Javascript 新手。请在力所能及的地方提供帮助。谢谢。