input输入框只能输入数字且首位不能为0

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .wrap{
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.4);
            display: flex;
            justify-content: center;
            align-items: center;
        }
        body,html{
            width: 100%;
            height: 100%;
        }
        .box{
            width: 65%;
            height: 180px;
            background: white;
            position: relative;
        }
        .num-p{
            border-bottom: 1px solid #ccc;
            height: 30%;
            width: 100%;

        }
        .num-p span{
            width: 15%;
            font-size: 30px;
            position: absolute;
            display: block;
            box-sizing: border-box;
            height: 30%;
            line-height: 170%;
            color: red;
        }
        .num-p span:first-child{
            left: 0;
            text-align: right;
        }
        .num-p span:last-child{
            right: 0;
            text-align: left;
        }
        .num-p input{
            width: 70%;
            height: 30%;
            box-sizing: border-box;
            outline: none;
            border: none;
            border-radius: 10px;
            left: 15%;
            font-size: 18px;
            position: absolute;
            display: block;
            text-align: center;
        }
        button{
            width: 80%;
            height: 35px;
            background: #ddd;
            border-radius: 5px;
            margin: 20px auto;
            display: block;
            border: none;
            outline: none;
            font-size: 15px;
        }
        .title-p{
            text-align: center;
            font-size: 18px;
            padding: 13px 0;
            overflow: hidden;
            white-space:nowrap;
            width: 81%;
            margin-left: 10%;
        }

    </style>
</head>

<body>
    <div class="wrap">
        <div class="box">
            <p class="title-p">商品名称商品名称商品名称商品名称商品名称商品名称</p>
            <p class="num-p">
                <span id="span1">-</span>
                <input type="text" placeholder="输入数量" id="inpval" >
                <span id="span2">+</span>
            </p>
            <button>确认</button>
        </div>
    </div>

</body>
<script>
    var input=document.getElementById('inpval');
    var span1=document.getElementById('span1');
    var span2=document.getElementById('span2');
    input.addEventListener('keyup',inputHander);
    span1.addEventListener('click',clickHander1);
    span2.addEventListener('click',clickHander2);
    function inputHander() {
        this.value=this.value.replace(/[^0-9-]+/,'');
        if(this.value<1){
            this.value=1
        }
    }
    function clickHander2() {
        input.value++
    }
    function clickHander1() {
        input.value--;
        if (input.value < 1) {
            input.value = 1
        }
    }
</script>
</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值