Javascript问题:将选中的项目拉出来并显示它们(如购物车)
好的,这是我的HTMLJavascript问题:将选中的项目拉出来并显示它们(如购物车),javascript,Javascript,好的,这是我的HTML <!--Enveloppe principale - wrapper --> <div id="wrapper"> <h1> Magasin de musique </h1> <form id="musictems" name="musicitems"> <!--Enveloppe magasin - storewrapper --> <div id="storewrapper">
<!--Enveloppe principale - wrapper -->
<div id="wrapper">
<h1> Magasin de musique </h1>
<form id="musictems" name="musicitems">
<!--Enveloppe magasin - storewrapper -->
<div id="storewrapper">
<div class="collections">
<h2>Collections de musique </h2>
<table>
<tr>
<th></th>
<th>Nom</th>
<th class="lrpadding">Prix</th>
<th>Quantité</th>
</tr>
<tr>
<td><input type="checkbox" value="135.50" name="collectioncheckboxes"/></td>
<td>Collection 1</td>
<td class="lrpadding">$135.50</td>
<td><input type="text" name="c1qty" size="4"></td>
</tr>
<tr>
<td><input type="checkbox" value="129.99" name="collectioncheckboxes"/></td>
<td>Collection 2</td>
<td class="lrpadding">$129.99</td>
<td><input type="text" name="c2qty" size="4"></td>
</tr>
<tr>
<td><input type="checkbox" value="145.99" name="collectioncheckboxes"/></td>
<td>Collection 3</td>
<td class="lrpadding">$145.99</td>
<td><input type="text" name="c3qty" size="4"></td>
</tr>
</table>
</div>
音乐杂志
音乐收藏
笔名
大奖赛
定量与定量;
集合1
$135.50
收藏2
$129.99
收藏3
$145.99
基本上只是项目的复选框和旁边的数量框。其目的是显示所有选中的项目,将其值乘以数量,并生成总数。
我之前问过这个问题,有人给了我一些有用的提示,但每次我尝试运行代码/函数时,什么都没有发生
以下是我的javascript:
function calculerAfficherDetailsAchats ()
{
var c1 = document.getElementsByName("collectioncheckboxes");
var totalcost = 0;
var header = "<table><tr><th class='pname'>Product Name</th>" +
"<th class='price'>Prix</th></tr>";
var body ="";
if(collectioncheckboxes[0].checked)
{
var price = parseFloat(collectioncheckboxes[0].value);
totalcost = totalcost + price
var fdesc = "item1";
body = body + "<tr><td class='fname'>"+fdesc +"</td>"+ "<td class='price'>"+price+"</td></tr>";
}
if(collectioncheckboxes[1].checked)
{
var price = parseFloat(collectioncheckboxes[1].value);
totalcost = totalcost + price;
var fdesc = "item2";
body = body + "<tr><td class='fname'>"+fdesc +"</td>"+ "<td class='price'>"+price+"</td></tr>";
}
if(collectioncheckboxes[2].checked)
{
var price = parseFloat(collectioncheckboxes[2].value);
totalcost = totalcost + price;
var fdesc = "item3";
body = body + "<tr><td class='fname'>"+fdesc +"</td>"+ "<td class='price'>"+price+"</td></tr>";
}
var footer = "<tr><td> Cout total </td><td class='totalcost'>" + totalcost +"$</td></tr></table>";
costinfo = header + body + footer;
totalcost.innerHTML = costinfo;
totalcost.style.display = "block";
}
函数CalculerAfficherDetailsChats()
{
var c1=document.getElementsByName(“CollectionCheckBox”);
var总成本=0;
var header=“产品名称”+
“大奖赛”;
var body=“”;
如果(集合复选框[0]。已选中)
{
var price=parseFloat(CollectionCheckBox[0].value);
总成本=总成本+价格
var fdesc=“item1”;
body=body+“”+fdesc+“”+price+“”;
}
如果(集合复选框[1]。选中)
{
var price=parseFloat(collectioncheckbox[1].value);
总成本=总成本+价格;
var fdesc=“item2”;
body=body+“”+fdesc+“”+price+“”;
}
如果(集合复选框[2]。选中)
{
var price=parseFloat(collectioncheckbox[2].value);
总成本=总成本+价格;
var fdesc=“项目3”;
body=body+“”+fdesc+“”+price+“”;
}
var footer=“Cout total”+总成本+“$”;
成本信息=页眉+正文+页脚;
totalcost.innerHTML=成本信息;
totalcost.style.display=“block”;
}
所以,是的,我只是不明白为什么一开始什么都没有发生!如果我能从中得到一些东西,那么我可以专注于乘以数量。。。感谢您的帮助 这是一个开始。至少它不会爆炸: 您需要学习使用调试器。代码中有几个JavaScript错误 你有一件奇怪的事:
var totalcost = 0;
//(SNIP)
totalcost = totalcost + price;
//(SNIP)
totalcost.innerHTML = costinfo;
totalcost.style.display = "block";
所以,您将totalcost存储在一个数字变量中,然后将其作为DOM元素处理。我假设你想要一个名为totalcost的div或者别的什么,你想要的是:
document.getElementById("totalCost").innerHTML = costinfo;
当然,您还需要一个totalCost div。正如我所说,这段代码有很多错误。尝试更改
CollectionCheckBox[0]
中的c1[0]
复选框,因为这是您给它的变量的名称,因为您定义了一个函数,并且。。。永远不要叫它?请把你的问题标题改成更有用的。我是说,请把它换掉。嘿!谢谢你,我确实把它改成了c1[0]等等,但还是没有运气。我确实按下了一个按钮来调用它。该按钮调用该函数,其代码经过测试并正常工作。这是实际的函数不能正常运行:(啊,非常感谢。感谢你的提示和代码;你的假设是正确的!我确实有很多错误,但我设法让它工作:)。非常感谢你的帮助。