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 ...
随机推荐
- 用递归调用实现字符串反转(java版)
写一个函数,输入int型,返回整数逆序后的字符串.如:输入123,返回“321”. 要求必须用递归,不能用全局变量,输入必须是一个参数,必须返回字符串. public static String re ...
- 左右手坐标系转换时R和T的具体形式分析
本文介绍了在计算机视觉的一些应用中,左手坐标系和右手坐标系之间转换时,旋转矩阵R和平移向量T的具体表达形式有哪些变化.
- infoq 微信后台存储架构
infoq 上微信后台存储架构 视频很是值得认真一听,大概内容摘要如下: 主要内容:同城分布式强一致,园区级容灾KV存储系统 - sync 序列号发生器 移动互联网场景下,频繁掉线重连,使用 ...
- tengine/nginx-tomcat动静分离遇到的问题
小站安装好tengine后,接下来的工作就是要配置好tengine让其和后端的tomcat正常的连接工作起来,tengine的配置文件本身比较简单,网上有大量的相关介绍说明文档,我这里只是摘出我配置过 ...
- 重建索引提高SQL Server性能
大多数SQL Server表需要索引来提高数据的访问速度,如果没有索引,SQL Server 要进行表格扫描读取表中的每一个记录才能找到索要的数据.索引可以分为簇索引和非簇索引,簇索引通过重排表中的数 ...
- [Sass]声明变量
[Sass]声明变量 定义变量的语法: 在有些编程语言中(如,JavaScript)声明变量都是使用关键词"var"开头,但是在 Sass 不使用这个关键词,而是使用大家都喜欢的美 ...
- Web服务器与数据库服务器分离 导入 Excel数据至数据库
一般情况一般项目WEB服务器与数据库均部署在一台服务器,文件上传,数据导入在一台服务器完成.web服务器与数据库服务器分离,文件上传与数据导入将分布在两台服务器或多台服务器之间.本案例为两台服务器,具 ...
- OpenGL中坐标系的理解(一)
在OpenGL中,存在着至少存在着三种矩阵,对应着函数glMatrixMode()的三个参数:GL_MODELVIEW,GL_PROJECTION,GL_TEXTURE. 以下主要描述GL_MODEL ...
- Div 定时移动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- MySql触发器语法总结
经过昨天多次失败,今天终于将我要实现的触发器功能写成功了,触发器代码如下: -- use dbfortest; drop trigger if exists tg_before_insert_on_d ...