function cart_number(txt_id, type, num)
{
num = num || 1;
var txt = document.getElementById(txt_id);
var source_num = parseInt(txt.value);
if(source_num == 1 && type == '-')
{
alert('请最少购买一个商品');
return;
}
var to_num = source_num;
if(type == '+')
{
to_num = source_num + 1;
}
else if(type == '-')
{
to_num = source_num - 1;
}
txt.value = to_num;
}
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
/**
* 购物车加减按钮
* @param txt_id 数量的ID
* @param type 加 + 减 -
* @param num 添加或者减少的数量 默认为一
*/
function cart_number(txt_id, type, num)
{
num = num || 1;
var txt = document.getElementById(txt_id);
var source_num = parseInt(txt.value);
if(source_num == 1 && type == '-')
{
alert('请最少购买一个商品');
return;
}
var to_num = source_num;
if(type == '+')
{
to_num += parseInt(num);
}
else if(type == '-')
{
to_num -= num;
}
txt.value = to_num;
}</script>
</head>
<body>
<label>
<input name="down" type="submit" id="down" value="-" onclick="cart_number('su','-','1');"/>
</label>
<label>
<input name="su" type="text" id="su" value="3" size="5" />
</label>
<input name="up" type="submit" id="up" value="+" onclick="cart_number('su','+','1');"/>
</body>
</html>