Here is My Code
Please have a look. It is displaying a single Item every time when a product is added to Cart, all I wants is to display single item once, and its total cost.
Please guide...
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Shoping cart </h2>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<img name="HP" id="imhp" class="img-thumbnail" src="http://placehold.it/150x150" onclick="addtoCart('imhp');" style="cursor:pointer;">
</div>
<div class="form-group">
<p>product small descriptioon</p>
</div>
<button type="submit" class="btn btn-default" style="margin: 0 0 15px;" onclick="addtoCart(1);">Add To cart</button>
</div>
<div class="col-md-3">
<div class="form-group">
<img name="Dell" id="imdell" class="img-thumbnail" src="http://placehold.it/150x150" onclick="addtoCart('imdell');" style="cursor:pointer;">
</div>
<div class="form-group">
<p>product small descriptioon</p>
</div>
<button type="submit" class="btn btn-default" style="margin: 0 0 15px;" onclick="addtoCart(2);">Add To cart</button>
</div>
<div class="col-md-3">
<div class="form-group">
<img name="sony" id="imsony" class="img-thumbnail" src="http://placehold.it/150x150" onclick="addtoCart('imsony');" style="cursor:pointer;">
</div>
<div class="form-group">
<p>product small descriptioon</p>
</div>
<button type="submit" class="btn btn-default" style="margin: 0 0 15px;" onclick="addtoCart(3);">Add To cart</button>
</div>
</div>
</div>
<div class="container">
<div class="jumbotron">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Qty</th>
<th>Amount</th>
</tr>
</thead>
<tbody id="cartt">
<tr>
<td></td>
<td></td>
<td></td>
<td colspan="3">Sum: 180</td>
</tr>
<button type="submit" class="btn btn-default" style="margin: 0 0 15px;" onclick="clr();">Cancel</button>
</tbody>
</table>
</div>
</div>
</div>
</body>
<script>
var qty =0;
var amount =0;
var naam='';
//var desc=$('#imhp').attr('name');
function addtoCart(id){
{
if(id=='imhp'){
amount = amount+10;
naam='HP';
}
else if(id=='imdell'){
amount = amount+20;
naam='Dell';
}
else if(id=='imsony'){
amount = amount+30;
naam='Sony';
}
}
qty=qty+1;
var ele= $("#cartt");
ele.append('<tr><td>'+naam+'</td><td>'+qty+'</td><td>'+amount+'</td></tr>');
naam='';
qty=0;
amount=0;
}
//clear function on Cancel button
function clr()
{
qty =0;
amount =0;
//following line will give a blank HTML on cart DIV
$("#cartt").html('');
}
</script>
</html>
Here you can also see the results after compiling. Attached Screen Shot Please have a look and guide me further how to achieve my target.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…