如何在用户输入的输入类型=文本中追加%符号?

时间:2016-07-22 07:22:55

标签: javascript jquery html css

我正在使用数字微调器,我希望输入在文本框中有%符号,默认情况下。我尝试使用span,但它在框外添加了符号。

3 个答案:

答案 0 :(得分:2)

您可以使用简单的css技巧,只需将其添加到您的代码中:

.spinner input {
  padding-right: 13px;
}

.spinner .input-group-btn-vertical::before {
  content: "%";
  position: absolute;
  right: 18px;
  top: 7px;
}

它使用 :: before 伪元素选择器将符号添加到文本框中。这是一个有效的例子:



(function ($) {
  $('.spinner .btn:first-of-type').on('click', function() {
    $('.spinner input').val( parseInt($('.spinner input').val(), 10) + 1);
  });
  $('.spinner .btn:last-of-type').on('click', function() {
    $('.spinner input').val( parseInt($('.spinner input').val(), 10) - 1);
  });
})(jQuery);

.spinner {
  width: 100px;
}
.spinner input {
  text-align: right;
}
.input-group-btn-vertical {
  position: relative;
  white-space: nowrap;
  width: 1%;
  vertical-align: middle;
  display: table-cell;
}
.input-group-btn-vertical > .btn {
  display: block;
  float: none;
  width: 100%;
  max-width: 100%;
  padding: 8px;
  margin-left: -1px;
  position: relative;
  border-radius: 0;
}
.input-group-btn-vertical > .btn:first-child {
  border-top-right-radius: 4px;
}
.input-group-btn-vertical > .btn:last-child {
  margin-top: -2px;
  border-bottom-right-radius: 4px;
}
.input-group-btn-vertical i{
  position: absolute;
  top: 0;
  left: 4px;
}

.spinner input {
  padding-right: 13px;
}

.spinner .input-group-btn-vertical::before {
  content: "%";
  position: absolute;
  right: 18px;
  top: 7px;
}

<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="page-header"><h1>Bootstrap 3 input-spinner</h1></div>  
  <div class="input-group spinner">
    <input type="text" class="form-control" value="42">
    <div class="input-group-btn-vertical">
      <button class="btn btn-default" type="button"><i class="fa fa-caret-up"></i></button>
      <button class="btn btn-default" type="button"><i class="fa fa-caret-down"></i></button>
    </div>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
&#13;
&#13;
&#13;

(也在fiddle

答案 1 :(得分:1)

检查一下:

&#13;
&#13;
(function ($) {
     var minNumber = -100;
     var maxNumber = 100;
     $('.spinner .btn:first-of-type').on('click', function() {
         if($('.spinner input').val() == maxNumber){
             return false;
         }else{
             $('.spinner input').val( parseInt($('.spinner input').val(), 10) + 5 +'%');
         }
     });

     $('.spinner .btn:last-of-type').on('click', function() {
         if($('.spinner input').val() == minNumber){
             return false;
         }else{
             $('.spinner input').val( parseInt($('.spinner input').val(), 10) -5+'%');
         }
     });
  
  $(".spinner input").on("input", function(){
    var intValue = parseInt($(this).val().replace(/%/g, '') ) || 0;
    
    $(this).val( intValue + '%' );
  });
    })(jQuery);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id=spinner class="container">
  <div class="input-group spinner">
    <input type="text" class="form-control" value="0">
    <div class="input-group-btn-vertical">
      <button class="btn btn-default" type="button"><i class="fa fa-caret-up" style="color: #C41C01">Up</i></button>
      <button class="btn btn-default" type="button"><i class="fa fa-caret-down" style="color: #20AD4E">Down</i></button>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用jQuery更改事件来完成此操作。它会添加&#39;%&#39;输入结束时的符号。

&#13;
&#13;
(function($) {
  var minNumber = -100;
  var maxNumber = 100;
  $('.spinner .btn:first-of-type').on('click', function() {
    if ($('.spinner input').val() == maxNumber) {
      return false;
    } else {
      $('.spinner input').val(parseInt($('.spinner input').val(), 10) + 5 + '%');
    }
  });

  $('.txtinput').on("change", function() {
    var inputVal = parseFloat($(this).val().replace('%', '')) || 0

    if (minNumber > inputVal) {
      inputVal = -100;
    } else if (maxNumber < inputVal) {
      inputVal = 100;
    }

    $(this).val(inputVal + '%');
  });

  $('.spinner .btn:last-of-type').on('click', function() {
    if ($('.spinner input').val() == minNumber) {
      return false;
    } else {
      $('.spinner input').val(parseInt($('.spinner input').val(), 10) - 5 + '%');
    }
  });
})(jQuery);
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=spinner class="container">
  <div class="input-group spinner">
    <input type="text" class="form-control txtinput" value="0">
    <div class="input-group-btn-vertical">
      <button class="btn btn-default" type="button"><i class="fa fa-caret-up" style="color: #C41C01"></i>
      </button>
      <button class="btn btn-default" type="button"><i class="fa fa-caret-down" style="color: #20AD4E"></i>
      </button>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;