简体   繁体   English

使用美元符号和逗号格式化货币输入字段

[英]Format currency input field with dollar sign & commas

I have a revenue input field in a javascript/jquery form:我在 javascript/jquery 表单中有一个收入输入字段:

  1. Need a dollar sign:before需要美元符号:之前
  2. add commas as the currency increases随着货币的增加添加逗号

I have a dollar sign showing via css, but issues centering it and ensuring the field entry point is next to it without overlapping.我有一个通过 css 显示的美元符号,但问题是居中并确保字段入口点在它旁边而不重叠。 Unsure how to do the commas.不确定如何使用逗号。 Any suggestions or tips are welcome!欢迎任何建议或提示!

HTML: HTML:

  <form id="rev-calculator">
  <label for="price">Monthly Revenue</label>
  <div class="fields">
    <input type="number" name="price" id="price" min="0" max="10000000000" required data-type="number"> </input>
    <br>
  </form>

CSS: CSS:

  <style>
      .body {
        text-align: left;
      }
      
      .fields {
        margin: 0 10px 0 0;
      }
      
      .fields:before {
        content: "$";
        text-align: center;
        position: relative;
        left:30px;
      }
      
      #price {
        border-radius: 5px;
        margin: 15px;
        padding: 10px;
        color: black;
      }
    </style>

JS: JS:

<script>
  $('#rev-calculator').on('click', 'button', function(e) {
    e.preventDefault();
    var price = $("#price").val();
    console.log(price);
  })
</script>

codepen: https://codepen.io/kedarPE/pen/JjroYyb代码笔: https://codepen.io/kedarPE/pen/JjroYyb

input field输入字段

Well here's a way, though in truth not as simple as I hoped when I started down this path.好吧,这是一种方法,尽管实际上并不像我开始走这条路时所希望的那么简单。 You can use Intl.NumberFormat to get the comma in there (according to locale).您可以使用Intl.NumberFormat在其中获取逗号(根据语言环境)。 To accomodate decimals, I sniff for them in the beginning and append them to the result.为了容纳小数,我在开头嗅探它们,然后将它们 append 嗅探到结果。

To allow for the comma, I made this a text field with a pattern attribute.为了允许使用逗号,我将其设为具有模式属性的文本字段。 Also, I adjusted your CSS to make it a little nicer looking with the $另外,我调整了你的 CSS 让它看起来更漂亮一点

 $('#price').keydown(function(e) { setTimeout(() => { let parts = $(this).val().split("."); let v = parts[0].replace(/\D/g, ""), dec = parts[1] let n = new Intl.NumberFormat('en-EN').format(v); n = dec?== undefined. n + ":" + dec; n. $(this);val(n); }) })
 .body { text-align: left; }.fields { margin: 0 10px 0 0; }.fields:before { content: "$"; text-align: center; position: relative; left: 35px; } #price { border-radius: 5px; margin: 15px; padding: 10px 10px 10px 20px; color: black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="rev-calculator"> <label for="price">Monthly Revenue</label> <div class="fields"> <input type="text" pattern="[0-9.,]+" name="price" id="price" required data-type="number" /> <br> </form>

I'm surprised the unique answer for this issue has a lot of votes because it has a tiny but major flaw: the event shouldn't be keydown, it should be keyup.我很惊讶这个问题的独特答案有很多选票,因为它有一个微小但主要的缺陷:事件不应该是 keydown,它应该是 keyup。 If you use keydown, it won't read the keys you are pressing at the moment but the previous one.如果您使用 keydown,它不会读取您当前按下的键,而是读取前一个键。 So, please update your answer.所以,请更新你的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM