Creating a Shopping Cart using only HTML/JavaScript

前端 未结 3 358
南笙
南笙 2020-12-24 03:59

I\'m not sure what to do in order to complete this project. I need to create a shopping cart that uses only one HTML page. I have the table set up showing what is being so

3条回答
  •  死守一世寂寞
    2020-12-24 04:36

    For a project this size, you should stop writing pure JavaScript and turn to some of the libraries available. I'd recommend jQuery (http://jquery.com/), which allows you to select elements by css-selectors, which I recon should speed up your development quite a bit.

    Example of your code then becomes;

    function AddtoCart() {
      var len = $("#Items tr").length, $row, $inp1, $inp2, $cells;
    
      $row = $("#Items td:first").clone(true);
      $cells = $row.find("td");
    
      $cells.get(0).html( len );
    
      $inp1 = $cells.get(1).find("input:first");
      $inp1.attr("id", $inp1.attr("id") + len).val("");
    
      $inp2 = $cells.get(2).find("input:first");
      $inp2.attr("id", $inp2.attr("id") + len).val("");
    
      $("#Items").append($row);
        }
    

    I can see that you might not understand that code yet, but take a look at jQuery, it's easy to learn and will make this development way faster.

    I would use the libraries already created specifically for js shopping carts if I were you though.

    To your problem; If i look at your jsFiddle, it doesn't even seem like you have defined a table with the id Items? Maybe that's why it doesn't work?

提交回复
热议问题