码迷,mamicode.com
首页 > Web开发 > 详细

js input输入数量控制

时间:2016-04-09 13:37:41      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

<div class="w-number" >

<a class="w-number-btn w-number-btn-minus" pro="minus" href="javascript:void(0);">-</a>

<input class="w-number-input" pro="input" type="text" value="5">

<a class="w-number-btn w-number-btn-plus" pro="plus" href="javascript:void(0);">+</a>

</div>

 

 

window.ONE_GOODS = {
status : 1,
period : 304080621,
goods : {
gid : 1851,
gname : ‘平安金‘,
price : 3688,
buyable : true,
buyUnit : 10
},
existingTimes : 3485
};

 

//数量输入框 window.ONE_GOODS
var $numInput = $(".w-number-input");
var oBuyNum = parseInt($numInput.val());
//-
$(".w-number-btn.w-number-btn-minus").click(function(){
var buyNum = $numInput.val() - $numInput.val() % ONE_GOODS.goods.buyUnit - ONE_GOODS.goods.buyUnit;
oBuyNum = buyNum >= ONE_GOODS.goods.buyUnit ? buyNum : ONE_GOODS.goods.buyUnit;
$numInput.val(oBuyNum);
});
//+
$(".w-number-btn.w-number-btn-plus").click(function(){
var buyNum = parseInt($numInput.val()) - $numInput.val() % ONE_GOODS.goods.buyUnit + ONE_GOODS.goods.buyUnit;
oBuyNum = buyNum <= ONE_GOODS.existingTimes ? buyNum : ONE_GOODS.existingTimes;
$numInput.val(oBuyNum);
});
//输入
$numInput.hover(function(){selectText($numInput[0],0,$numInput.val().length);},function(){});
$(".w-number-input").change(function(){
var buyNum = $numInput.val();
if(/^\d+$/.test(buyNum)){
if(buyNum < ONE_GOODS.goods.buyUnit){
oBuyNum = ONE_GOODS.goods.buyUnit;
}else if( buyNum > ONE_GOODS.existingTimes){
oBuyNum = ONE_GOODS.existingTimes;
}else if(buyNum%ONE_GOODS.goods.buyUnit > 0){
oBuyNum = buyNum > ONE_GOODS.goods.buyUnit ? buyNum - buyNum % ONE_GOODS.goods.buyUnit + ONE_GOODS.goods.buyUnit : ONE_GOODS.goods.buyUnit;
}
}
$numInput.val(oBuyNum);
});

js input输入数量控制

标签:

原文地址:http://www.cnblogs.com/mengwei-ziyun/p/5371325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!