• js input输入数量控制


    <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);
    });

  • 相关阅读:
    让小车再飞一会儿 ——记校赛惨痛失败之旅
    排序算法之基数排序
    开博感言
    智能车 SCI实验
    三种算法求解一个数组的子数组最大和
    继续大话考研
    智能车实验室阶段测验之单片机基础
    怎样判断自己掌握了学到的新知识
    排序算法之计数排序
    进程与线程的区别?
  • 原文地址:https://www.cnblogs.com/mengwei-ziyun/p/5371325.html
Copyright © 2020-2023  润新知