js 实现商品加减

1.html:代码

  1. <div id="container">
    			<table>
    				<tr>					
    					<td class="p-name">炊大皇牛排煎锅26cm健康无涂层厚铸铁煎盘条棱纹牛扒盘电磁炉通</td>
    					<td> 
    						<div class="quantity-form">
    							<a href="javascript:void(0)" class="decrement">-</a><input type="text"  class="itxt"/><a href="javascript:void(0)" class="increment">+</a>
    						</div>
    					</td>
    				</tr>
    				<tr>					
    					<td class="p-name">【京东超市】爱仕达 32CM油你控新不粘少烟炒锅CL32Y5WT-C 不沾锅少油烟电磁</td>
    					<td>
    						<div class="quantity-form">
    							<a href="javascript:void(0)" class="decrement">-</a>
    							<input type="text"  class="itxt"/>
    							<a href="javascript:void(0)" class="increment">+</a>
    						</div>
    					</td>
    				</tr>
    				<tr>					
    					<td class="p-name">华为 Mate9 Pro 4G手机 双卡双待 保时捷版 全网通6GB +256GB版</td>
    					<td>
    						<div class="quantity-form">
    							<a href="javascript:void(0)" class="decrement">-</a><input type="text"  class="itxt"/><a href="javascript:void(0)" class="increment">+</a>
    						</div>
    					</td>
    				</tr>
    				<tr>					
    					<td class="p-name">双立人(ZWILLING) Twin Point 红点中式菜刀 磨刀石(红色)两件套装 德国进口</td>
    					<td>
    						<div class="quantity-form">
    							<a href="javascript:void(0)" class="decrement">-</a>
    							<input type="text"  class="itxt"/>
    							<a href="javascript:void(0)" class="increment">+</a>
    						</div>
    					</td>
    				</tr>
    			</table>
    		</div>

    2.css:代码

<style>
			body,p,div,h1,h2,h3,h4,h5,h6,ul,ol,li,dl,dt,dd{
				margin: 0;
				padding: 0;
			}
			ul,ol{
				list-style: none;
			}
			a{
				color: #000;
				text-decoration: none;
			}
			body{
				font: 12px/150% tahoma,arial,Microsoft YaHei,Hiragino Sans GB,"\u5b8b\u4f53",sans-serif;
			}
			table{
				width: 100%;
				border-collapse: collapse;
				border-spacing: 0;
			}
			input{
				outline: none;
			}
			#container{
				width: 90%;
				margin: 0 auto;
			}
			#container td{
				padding:10px;
				border:1px solid #ccc;
			}
			.p-name{
				width: 330px;
			}
			.quantity-form {
			    position: relative;
			    overflow: hidden;
			    height: 22px;
			    width: 80px;
			}			
			.increment,.decrement{
				border: 1px solid #cacbcb;
			    border-right: 0;
			    height: 18px;
			    line-height: 18px;
			    padding: 1px 0;
			    width: 16px;
			    text-align: center;
			    color: #666;
			    margin: 0;
			    background: #fff;
			}
			.increment{
			   float: right;
			   border: 1px solid #cacbcb;
			   border-left: 0;
			   color: #666;
			}
			.decrement {
 			   float: left;
			}
			
			.decrement.disabled, .increment.disabled {
    			cursor: default;
    			color: #e9e9e9;
			}
			.itxt {			    
			    border: 1px solid #cacbcb;
			    width: 42px;
			    height: 18px;
			    line-height: 18px;
			    text-align: center;
			    padding: 1px;
			    margin: 0;
			    font-size: 12px;
			    font-family: verdana;
			    color: #333;	
						    
			}
		</style>

 3.重点  来啦    JS  代码:

 

<script src="scripts/jquery-1.10.0.min.js"></script>
		<script>
			$(function(){
				//初始化
				$('.itxt').val('1');//获取文本框的值
				

				//加
				$('.increment').click(function(){
					$(this).prev().val(parseInt($(this).prev().val())+1);
					//单击加号时候  当前节点的上一个节点的值   等于   当前节点的上一个节点的值+1
					//$(this) 当前对象
					//prev() 上一个节点
					//parseInt 把文本框中的值 转换为整型(int)  如何不转换的话 获取到的可能是字符串(string)
					//如果获取到的是字符串  ,那么 文不框中的值会是  字符串+1   
				});

			

				//减
				$('.decrement').click(function(){
					$(this).next().val(parseInt($(this).next().val())-1);
					//和上面一样的 
					if( $('.itxt').val() <= 1 ){
						$('.itxt').val('1');//文本框中的值不能小于1  否则就等于1
					}
					
				});
				
			
			})
		</script>

不要忘记引入jQ插件;

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
商品加减实现通常需要以下几个步骤: 1. 在页面上显示商品数量加减按钮。 2. 点击加按钮时,通过 JavaScript 代码将商品数量加1,并更新页面上的数量显示。 3. 点击减按钮时,先判断商品数量是否大于0,如果是,则将数量减1,并更新页面上的数量显示。 4. 在更新数量的同时,还需要根据商品数量动态计算商品总价,并更新页面上的总价显示。 以下是一个简单的示例代码: HTML: ```html <div> <span>数量:</span> <span id="count">0</span> <button onclick="increment()">加</button> <button onclick="decrement()">减</button> </div> <div> <span>单价:</span> <span id="price">10</span> </div> <div> <span>总价:</span> <span id="total">0</span> </div> ``` JavaScript: ```javascript var countEl = document.getElementById("count"); var priceEl = document.getElementById("price"); var totalEl = document.getElementById("total"); var count = 0; function increment() { count++; countEl.innerText = count; updateTotal(); } function decrement() { if (count > 0) { count--; countEl.innerText = count; updateTotal(); } } function updateTotal() { var price = parseFloat(priceEl.innerText); var total = price * count; totalEl.innerText = total; } ``` 在这个示例中,我们使用了三个 `span` 元素来显示商品数量、单价和总价,以及两个 `button` 元素来触发加和减操作。每次点击加或减按钮时,都会调用对应的 JavaScript 函数来更新数量、总价以及页面显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值