简体   繁体   English

使用javascript显示简单的购物车(提价*数量)?

[英]Display simple shopping cart using javascript (pull up value * quantity)?

I have an open-book assignment I have to do regarding shopping carts via javascript and I can't seem to figure it out. 我有一个关于通过javascript进行购物车的开卷作业,我似乎无法弄清楚。 First, my HTML code looks like this: 首先,我的HTML代码如下所示:

<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&eacute;</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>

So the value is in the HTML itself. 因此,值在HTML本身中。 Now basically there's a button that when clicked runs a javascript function that I must code to display whatever items are checked, and multiplies the value of the checked items by the quantity (named c1qty/c2qty/etc). 现在基本上有了一个按钮,当单击该按钮时,它将运行一个javascript函数,我必须对其进行编码以显示所有已检查的项目,并将已检查项目的值乘以数量(名为c1qty / c2qty / etc)。

How can I do that without modifying the html? 我该如何做而不修改HTML? Any help or tips much appreciated! 任何帮助或提示,不胜感激!

Your function should use the DOM to grab the appropriate data from the HTML and then perform the multiplication operation upon it. 您的函数应使用DOM从HTML抓取适当的数据,然后对其执行乘法运算。 After this is done, you can use the innerHTML function to change the "text" values. 完成此操作后,您可以使用innerHTML函数更改“文本”值。

For an example/tutorial of changing values, see http://www.w3schools.com/js/js_htmldom_html.asp 有关更改值的示例/教程,请参见http://www.w3schools.com/js/js_htmldom_html.asp

EDIT: You could use the getElementsByClassName("lrpadding") method to pull down a list of all the tags of the specified class, and then iterate through the pertinent ones using the innerHTML property to read and then modify the prices based upon your multiplier. 编辑:您可以使用getElementsByClassName(“ lrpadding”)方法下拉指定类别的所有标记的列表,然后使用innerHTML属性遍历相关标记以读取并根据乘数修改价格。 http://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp http://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

You could use the getElementsByName("collectioncheckboxes") to create a collection of all the checkboxes in the document and then iterate through them using the isChecked property to see if the particular box is checked. 您可以使用getElementsByName(“ collectioncheckboxes”)创建文档中所有复选框的集合,然后使用isChecked属性进行遍历,以查看是否选中了该复选框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM