【问题标题】:Increment and decrement a number with Bootstrap design使用 Bootstrap 设计增加和减少一个数字
【发布时间】:2018-07-25 19:49:42
【问题描述】:

我正在尝试获得如下所示的 UI,并且需要在数字下方添加一个边框,与图片中的完全相同:

但是使用下面显示的代码,我得到了不同的结果。有人可以帮我设计这个吗?

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  .input-number{
  margin: 0 15px;
width: 50px !important;
}
  </style>
</head>
<body>
<div style="width:150px;padding:50px;">
<div class="input-group">
  <span class="input-group-btn">
    <button type="button" class="btn btn-default btn-number" disabled="disabled" data-type="minus" data-field="quant[1]">
      <span class="glyphicon glyphicon-minus"></span>
    </button>
  </span>
  <input type="text" name="quant[1]" class="form-control input-number" value="1" min="1" max="10">
  <span class="input-group-btn">
    <button type="button" class="btn btn-default btn-number" data-type="plus" data-field="quant[1]">
      <span class="glyphicon glyphicon-plus"></span>
    </button>
  </span>
  </div>
</div>
</body>
</html>

【问题讨论】:

    标签: jquery twitter-bootstrap css responsive-design


    【解决方案1】:

    您可以简单地将输入字段类型设置为“数字”,它会自动为您添加递增和递减按钮.. 这是一个代码示例:

    <input type="number" class="form-control" id="input1" placeholder="enter amount">
    

    【讨论】:

    • 不在 Chrome 中,仅在 Firefox 中。
    【解决方案2】:

    试试这个:

    .input-number{
      
        width: 50px !important;
        text-align: center!important;
        font-weight: bold!important;
        font-size: 25px!important;
        height: 40px!important;
        border:none!important;
        border-bottom: solid 2px #ccc!important;
        box-shadow: none!important;
        position:relative;
    }
    .input-container{
        position: relative;
        height: 40px;
        display: block; 
        margin: 0 15px;
    }
    .input-container:before{
        content: "";
        position: absolute;
        bottom: 0;
        left: 0;
        width: 2px;
        height: 10px;
        background-color: #ccc!important;
        z-index:10;
    }
    .input-container:after{
        content: "";
        position: absolute;
        bottom: 0;
        right: 0;
        width: 2px;
        height: 10px;
        background-color: #ccc!important;
        z-index:10;
    }
    .btn-number{
        border-radius: 50%!important;
        background-color: #ccc!important;
        color: #fff!important;
        outline: none!important;
        font-size: 25px!important;
        padding: 3px!important;
        width: 40px;
        height: 40px;
        
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    <div style="width:150px;padding:50px;">
    <div class="input-group">
      <span class="input-group-btn">
        <button type="button" class="btn btn-default btn-number" disabled="disabled" data-type="minus" data-field="quant[1]">
          <span class="glyphicon glyphicon-minus"></span>
        </button>
      </span>
      <span class="input-container">
      <input type="text" name="quant[1]" class="form-control input-number" value="1" min="1" max="10">
      </span>
      <span class="input-group-btn">
        <button type="button" class="btn btn-default btn-number" data-type="plus" data-field="quant[1]">
          <span class="glyphicon glyphicon-plus"></span>
        </button>
      </span>
      </div>

    【讨论】:

    • 我也有这个设计,但是没有得到输入数字的边框,需要和图片一样的边框
    • 你看到这个答案了吗?
    【解决方案3】:

    你必须像这样为btninput-numberclasses 赋予样式:

    .btn{
      height: 50px;
      width: 50px;
      border-radius: 50%,
      background-color: #ccc;
    }
    .btn .glyphicon {
      color: #fff;
    }
    
    .input-number {
      margin: 0 15px;
      width: 50px !important;
      border: none;
    }
    .input-number: after {
      content: '';
      position: absolute;
      bottom: 0;
      height: 3px;
      width: 50px;
      background-color: #fff;
      left: 50% - 50px;
    }
    

    【讨论】:

    • 数字下方的边框怎么样?
    • 为此我给出了样式.input-number:after,它在底部为您的号码设计了5px height边框。
    • 不行,你可以为你的答案更新一个新的小提琴
    • 抱歉,由于时间不够,如果您能提供的话,我不能提供,我一定会做出改变。
    【解决方案4】:

    input.btneg {
      border-radius: 5cm;
      width: 40px;
      position: relative;
      margin-right: -30px;
      height: 0.8cm;
    }
    
    input.btpos {
      border-radius: 5cm;
      width: 40px;
      position: relative;
      margin-left: -30px;
      height: 0.8cm;
    }
    <input type="button" class="btneg " value="-">
    <input type="text" name="noq1" id="noq1" min="1" max="15" value="12">
    <input type="button" class="btpos " value="+">

    【讨论】:

    • 虽然这可能会回答这个问题,但如果可能的话,您应该edit 您的回答包括一个简短的解释如何这个 sn-p 回答这个问题。这有助于提供上下文,并使您的答案对未来的读者更有用。
    【解决方案5】:
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    
    <!------ Include the above in your HEAD tag ---------->
    
    <div class="container">
        <div class="row">
            <h2>Simple Quantity increment buttons with Javascript </h2>
            
            
                            <div class="col-lg-2">
                                            <div class="input-group">
                                        <span class="input-group-btn">
                                            <button type="button" class="quantity-left-minus btn btn-danger btn-number"  data-type="minus" data-field="">
                                              <span class="glyphicon glyphicon-minus"></span>
                                            </button>
                                        </span>
                                        <input type="text" id="quantity" name="quantity" class="form-control input-number" value="10" min="1" max="100">
                                        <span class="input-group-btn">
                                            <button type="button" class="quantity-right-plus btn btn-success btn-number" data-type="plus" data-field="">
                                                <span class="glyphicon glyphicon-plus"></span>
                                            </button>
                                        </span>
                                    </div>
                            </div>
        </div>
    </div>
    <script>
    $(document).ready(function(){
    
    var quantitiy=0;
       $('.quantity-right-plus').click(function(e){
            
            // Stop acting like a button
            e.preventDefault();
            // Get the field name
            var quantity = parseInt($('#quantity').val());
            
            // If is not undefined
                
                $('#quantity').val(quantity + 1);
    
              
                // Increment
            
        });
    
         $('.quantity-left-minus').click(function(e){
            // Stop acting like a button
            e.preventDefault();
            // Get the field name
            var quantity = parseInt($('#quantity').val());
            
            // If is not undefined
          
                // Increment
        if(quantity>0){
        $('#quantity').val(quantity - 1);
    }
    });
        
    });
    </script>
    

    【讨论】:

      【解决方案6】:

      试试看Live Demo检查

      function increaseValue() {
        var value = parseInt(document.getElementById('number').value, 10);
        value = isNaN(value) ? 0 : value;
        value++;
        document.getElementById('number').value = value;
      }
      
      function decreaseValue() {
        var value = parseInt(document.getElementById('number').value, 10);
        value = isNaN(value) ? 0 : value;
        value < 1 ? value = 1 : '';
        value--;
        document.getElementById('number').value = value;
      }
      form {
        width: 300px;
        margin: 0 auto;
        text-align: center;
        padding-top: 50px;
      }
      
      .value-button {
        display: inline-block;
        border: 1px solid #ddd;
        margin: 0px;
        width: 40px;
        height: 20px;
        text-align: center;
        vertical-align: middle;
        padding: 11px 0;
        background: #eee;
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
      }
      
      .value-button:hover {
        cursor: pointer;
      }
      
      form #decrease {
        margin-right: -4px;
        border-radius: 8px 0 0 8px;
      }
      
      form #increase {
        margin-left: -4px;
        border-radius: 0 8px 8px 0;
      }
      
      form #input-wrap {
        margin: 0px;
        padding: 0px;
      }
      
      input#number {
        text-align: center;
        border: none;
        border-top: 1px solid #ddd;
        border-bottom: 1px solid #ddd;
        margin: 0px;
        width: 40px;
        height: 40px;
      }
      
      input[type=number]::-webkit-inner-spin-button,
      input[type=number]::-webkit-outer-spin-button {
          -webkit-appearance: none;
          margin: 0;
      }
      <form>
        <div class="value-button" id="decrease" onclick="decreaseValue()" value="Decrease Value">-</div>
        <input type="number" id="number" value="0" />
        <div class="value-button" id="increase" onclick="increaseValue()" value="Increase Value">+</div>
      </form>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-13
        相关资源
        最近更新 更多