js实现输入框数量加减【转】
<!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>js数量加减</title>
<script type="text/javascript" src="http://www.jb51.net/Public/js/jquery.min.js"></script> <script type="text/javascript">
/*或者不用jquery*/
/*商品数量框输入*/
function keyup(){
var quantity = document.getElementById("quantity").value;
if(isNaN(quantity) || parseInt(quantity)!=quantity || parseInt(quantity)<1){
quantity = 1;
return;
}
if(quantity>=10){
document.getElementById("quantity").value=quantity.substring(0,quantity.length-1);
alert("商品数量不能大于10");
}
} /*商品数量+1*/
function numAdd(){
var quantity = document.getElementById("quantity").value;
var num_add = parseInt(quantity)+1;
var price=document.getElementById("price").value;
if(quantity==""){
num_add = 1;
}
if(num_add>=10){
document.getElementById("quantity").value=num_add-1;
alert("商品数量不能大于10");
}else{
document.getElementById("quantity").value=num_add;
var Num=price*num_add;
document.getElementById("totalPrice").innerHTML=Num.toFixed(2);
}
}
/*商品数量-1*/
function numDec(){
var quantity = document.getElementById("quantity").value;
var price=document.getElementById("price").value;
var num_dec = parseInt(quantity)-1;
if(num_dec>0){
document.getElementById("quantity").value=num_dec;
var Num=price*num_dec;
document.getElementById("totalPrice").innerHTML=Num.toFixed(2);
}
}
function show()
{
document.getElementById("totalPrice").innerHTML=3.25*3;
}
</script> </head>
<body>
<p>Quantity: <span onclick="numDec()">-</span> <input type="text" id="quantity" /> <span onclick="numAdd()">+</span></p>
<p class="sdsd">Total Price: <span id="totalPrice">28.10</span></p>
<input type="hidden" value="28.1" id="price" /><!--单价-->
<input type="button" value="展示" onclick="show()"/>
</body>
</html>
第二种方法:不包含计算价格
<script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var price=0;
$("#cut").click(function(){
price=$("#num").val();
if($("#num").val()==0){
return
}
else{
price--;
}
$("#num").val(price);
})
$("#add").click(function(){
price=$("#num").val();
price++;
$("#num").val(price);
})
})
</script>
<input id="cut" type="button" value="-"/>
<input id="num" type="text" style=" width:20px; text-align:center" value="0"/>
<input id="add" type="button" value="+"/>
方法3:
<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 = c;
}
</script> <FORM name="form1" >
<A href="javascript:qtyUpdate('down')">-</A>
<INPUT value=2 readOnly size=5 name="qty">
<A href="javascript:qtyUpdate('up')">+</A>
</FORM> <script type="text/JavaScript">
function qtyUpdate11(kind){
var f = document.ipu;
var c = f.qcc.value;
if(kind == "jia"){
c++;
}else if(kind == "jian"){
if(c > 1) c--;
}
f.qcc.value = c;
}
</script> <FORM name="ipu" >
<A href="javascript:qtyUpdate11('jian')">-</A>
<INPUT value="0" readOnly size="5" name="qcc">
<A href="javascript:qtyUpdate11('jia')">+</A>
</FORM>
js实现输入框数量加减【转】的更多相关文章
- js实现购买数量加减效果
写在前面:当我们需要在多个页面都有操作数量的需求时的一种解决方案 结构: js代码: <script type="text/javascript"> function ...
- js 购物车的数量加减,对应的总价也随机变化
html相关的源码: <div class="goods_num clearfix"> <div class="num_name fl"> ...
- 自己动手丰衣足食之 jQuery 数量加减插件
引言 做一个手机端的订单相关项目中,其中下订单时需要用到数量加减的控件,可以设置默认值,也可以设置最大值和最小值.使用jQuery这么长时间了,平时很少去编写属于自己的插件,现在编写的时候对立面的一些 ...
- web框架实现购物车数量加减
企业开发中经常是团队协作,每个人分配一个小的模块,比如说购物车模块,数量加减这一块人们首先想到的就是通过jquery实现,其实作为一个后端接口开发的程序猿也可以用自己更擅长的后端的逻辑代码来实现,那我 ...
- js jquery 权限单选 bug修改以及正确代码 购物车数量加减
效果图废话不多直接上代码 用的avalon渲染,其实都是一样的 <div class="shop-arithmetic"> <a href="javas ...
- vue 入门 ------简单购物车功能实现(全选,数量加减,价格加减)
简易购物车功能(无任何布局 主要是功能) 数量的加减 商品的总价钱 全选与全不选 删除(全选.价格 受影响) <script src="https://cdn.jsdelivr.net ...
- 用js进行日期的加减
如题,开始查了查js的使用文档,但没发现可以直接用的函数,于是就想自己写函数来着,这就要涉及到每个月天数的判断,如果是2月份的话,还要涉及到闰年的判断,虽然不复杂但我想js应该不会这么低级,于是查了下 ...
- Android 自定义控件 EditText输入框两边加减按钮Button
自己封装的一个控件:EditText两边放加减按钮Button来控制输入框的数值 Demo 下载地址: 第一版:http://download.csdn.net/detail/zjjne/674086 ...
- Js中处理日期加减天数
Js的处理日期还是很方便的. 一. 格式化日期为2017-07-04的格式 function formatTime(date) { var year = date.getFullYear(); var ...
随机推荐
- JavaScript的==和===运算符
JavaScript提供两个相等运算符:==和 ===. 简单说,它们的区别是相等运算符( ==)比较两个值是否相等,严格相等运算符( ===)比较它们是否为“同一个值”.如果两个值不是同一 ...
- Jni 调试 : eclipse + Vs 联合调试
摘要: 本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4982640.html 1.在Eclipse 中,Java 类中链接库引用到vs的debug目录下 ...
- 1.Two Sum(c++)(附6ms O(n) accepted 思路和代码)
问题描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...
- 20个Mac用户必须掌握的触摸手势
我第一次接触MacBook时,最令我惊叹的就是MacBook的触摸板,通过各种手势,完全可以不用鼠标,且有些时候更加的快捷和方便.那么都有哪些手势呢?可以通过 -> 来查看学习各种手势的使用,下 ...
- 【Network】TCPDUMP 详解
参考资料: https://www.baidu.com/s?ie=UTF-8&wd=tcpdump%20%E6%8C%87%E5%AE%9Aip tcpdump非常实用的抓包实例: http ...
- pycharm2016 激活
pycharm 2016 专业版 激活方式选第二种 code 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibG ...
- repo 修改邮箱地址
需要重新运行 repo init 被带上参数: --config-name xx@a.com
- 关于目录路径path
1.框架的目录结构 yii-1 .htaccess index.php protect -config -controllers -components 2.Linux 服务器 CentOs usr/ ...
- nagios检测http
/usr/local/nagios/etc/server/下相应的地址检测加上以下一段 (server下的cfg文件是检测相应服务器的模块) define service{ use ...
- QT插件使用
1基本插件制作流程 1) 定义接口.接口定义为抽象基类,如IPluginInterface,实现高度封装.定义的头文件在最后需要通过Q_DECLARE_INTERFACE来唯一标识该接口,即通过一个全 ...