首页 > 代码库 > js实现购物车数量的自增与自减
js实现购物车数量的自增与自减
<script type="text/JavaScript"> function qtyUpdate(kind){ var f = document.form1; var c = f.qty.value; if(kind == "up"){ c++; }else if(kind == "down"){ if(c > 1) c--; } f.qty.value = http://www.mamicode.com/c; } </script>
<FORM name=form1 > <TABLE border=0 cellSpacing=0 cellPadding=0> <TBODY> <TR> <TD rowSpan=2><INPUT value=http://www.mamicode.com/2 readOnly size=5 name=qty>
或
<script type="text/javascript" src="http://www.mamicode.com/wang_7380031@126/blog/js/jquery-1.4.2.min.js"></script>
<script type="text/JavaScript">
function qtyUpdate(kind,id){
var num = parseInt($(‘#amount_‘+id).val());
if(kind == "up"){
$(‘#amount_‘+id).val(num+1);
}else{
if(num>1){
$(‘#amount_‘+id).val(num-1);
}
}
}
</script>
<A href="javascript:qtyUpdate(‘down‘,‘<?php echo $key; ?>‘)" style="margin-top:3px;"><IMG src="http://www.mamicode.com/wang_7380031@126/blog/images/down1.jpg"></A>
<input id=‘amount_<?php echo $key;?>‘ value="http://www.mamicode.com/1" type="text" name=‘qty[]‘ size="3">
<A href="javascript:qtyUpdate(‘up‘,‘<?php echo $key; ?>‘)" ><IMG src="http://www.mamicode.com/wang_7380031@126/blog/images/up1.jpg"></A>
js实现购物车数量的自增与自减