Set value to currency in <input type=“number” />

后端 未结 5 2036
逝去的感伤
逝去的感伤 2020-12-01 01:35

I am trying to format a number input by the user into currency using javascript. This works fine on . However, on

相关标签:
5条回答
  • 2020-12-01 02:12

    It seems that you'll need two fields, a choice list for the currency and a number field for the value.

    A common technique in such case is to use a div or span for the display (form fields offscreen), and on click switch to the form elements for editing.

    0 讨论(0)
  • 2020-12-01 02:14

    You guys are completely right numbers can only go in the numeric field. I use the exact same thing as already listed with a bit of css styling on a span tag:

    <span>$</span><input type="number" min="0.01" step="0.01" max="2500" value="25.67">
    

    Then add a bit of styling magic:

    span{
      position:relative;
      margin-right:-20px
    }
    input[type='number']{
      padding-left:20px;
      text-align:left;
    }
    
    0 讨论(0)
  • 2020-12-01 02:27

    In the end I made a jQuery plugin that will format the <input type="number" /> appropriately for me. I also noticed on some mobile devices the min and max attributes don't actually prevent you from entering lower or higher numbers than specified, so the plugin will account for that too. Below is the code and an example:

    (function($) {
      $.fn.currencyInput = function() {
        this.each(function() {
          var wrapper = $("<div class='currency-input' />");
          $(this).wrap(wrapper);
          $(this).before("<span class='currency-symbol'>$</span>");
          $(this).change(function() {
            var min = parseFloat($(this).attr("min"));
            var max = parseFloat($(this).attr("max"));
            var value = this.valueAsNumber;
            if(value < min)
              value = min;
            else if(value > max)
              value = max;
            $(this).val(value.toFixed(2)); 
          });
        });
      };
    })(jQuery);
    
    $(document).ready(function() {
      $('input.currency').currencyInput();
    });
    .currency {
      padding-left:12px;
    }
    
    .currency-symbol {
      position:absolute;
      padding: 2px 5px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <input type="number" class="currency" min="0.01" max="2500.00" value="25.00" />

    0 讨论(0)
  • 2020-12-01 02:29

    Add step="0.01" to the <input type="number" /> parameters:

    <input type="number" min="0.01" step="0.01" max="2500" value="25.67" />
    

    Demo: http://jsfiddle.net/uzbjve2u/

    But the Dollar sign must stay outside the textbox... every non-numeric or separator charachter will be cropped automatically.

    Otherwise you could use a classic textbox, like described here.

    0 讨论(0)
  • 2020-12-01 02:32

    The browser only allows numerical inputs when the type is set to "number". Details here.

    You can use the type="text" and filter out any other than numerical input using JavaScript like descripted here

    0 讨论(0)
提交回复
热议问题