Js点餐加减数量

时间:2022-10-17 18:36:46
       <button class="add-on" onclick="chgNum(1,'del')" ><i class="icon-minus"></i></button>

       <input class="span3 text-center" id="appendedPrependedInput1" disabled="disabled" type="text" value="">

       <button class="add-on" onclick="chgNum(1,'add')"><i class="icon-plus"></i></button>
<script>
function chgNum(ndid, opr){ /** 点餐加减数量,参照以前的**/
var index= parseInt($('#appendedPrependedInput'+ndid).val());
var price = $('.price').html().substring();
if(opr=='del'){ if(index == ){
alert("不能减了!");
return ;
}
$("#appendedPrependedInput"+ndid).val(index - );
index = index - ;
}else if(opr=='add'){
if(index == ){
alert('不能加了!');
return ;
}
$("#appendedPrependedInput"+ndid).val(index + );
index = index + ; }
$('.nums').html(index);
$('.total').html(index*price+'元')
}
</script>

第二种方法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script> $(function(){
$(".add").click(function(){
var t=$(this).parent().find('input[class*=text_box]');
t.val(parseInt(t.val())+)
setTotal();
}) $(".min").click(function(){
var t = $(this).parent().find('input[class*=text_box]');
t.val(parseInt(t.val())-)
if(parseInt(t.val())<){
t.val();
}
setTotal();
}) function setTotal(){
var s=;
$("#tab td").each(function(){
s +=parseInt($(this).find('input[class*=text_box]').val())*parseFloat($(this).find('span[class*=price]').text());
});
$("#total").html(s.toFixed());
} setTotal(); }) </script>
</head>
<body>
<table id="tab">
<tr>
<td>
<span>单价:</span><span class="price">1.50</span>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="" />
<input class="add" name="" type="button" value="+" />
</td>
</tr>
<tr>
<td>
<span>单价:</span><span class="price">3.95</span>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="" />
<input class="add" name="" type="button" value="+" />
</td>
</tr>
</table> <p>总价:<label id="total"></label></p> </body>
</html>