通过类名返回输入字段的值

huangapple go评论55阅读模式
英文:

Return value to input field by class name

问题

以下是翻译好的部分:

所以,在我的代码中,我有多个包括currency类的输入字段,如下所示:

<input class="form-textbox number-only currency" type="text" id="nilai1" required/>
<input class="form-textbox number-only currency" type="text" id="nilai2" required/>

我成功找到了一个JavaScript函数来将数字格式更改为货币格式:

function formatRupiah(nominal){
    return new Intl.NumberFormat('id-ID',
        { style: 'currency', currency: 'IDR', minimumFractionDigits: 0 }
    ).format(nominal);
}

并且使用下面的这个函数,我能够在控制台中显示值:

$('.rupiah').keyup(function(){
    console.log(formatRupiah(this.value));
});

但是,我无法找到一种方法来根据用户输入的输入字段返回值,因为我有多个具有currency类的输入字段。有没有办法根据输入字段的类返回值?谢谢。

英文:

So I have multiple input fields in my code which include currency class as following

&lt;input class=&quot;form-textbox number-only currency&quot; type=&quot;text&quot; id=&quot;nilai1&quot; required/&gt;
&lt;input class=&quot;form-textbox number-only currency&quot; type=&quot;text&quot; id=&quot;nilai2&quot; required/&gt;

I managed to find a javascript function to change the number format to currency

function formatRupiah(nominal){
    return new Intl.NumberFormat(&#39;id-ID&#39;,
        { style: &#39;currency&#39;, currency: &#39;IDR&#39;, minimumFractionDigits: 0 }
    ).format(nominal);
}

And with this function below, I am able to show the value in console

$(&#39;.rupiah&#39;).keyup(function(){
    console.log(formatRupiah(this.value));
});      

But I couldn't find a way to return the value to my input fields because I have multiple input fields with currency class. Is there any way to return the value by class according to which input fields the users are typing? Thank you.

答案1

得分: 3

以下是翻译好的部分:

你可以使用this来引用触发事件的特定输入字段,如以下方式:

function formatRupiah(nominal){
  return new Intl.NumberFormat('id-ID',
    { style: 'currency', currency: 'IDR', minimumFractionDigits: 0 }
  ).format(nominal);
}

$('.currency').keyup(function(){
  //仅获取数字
  var numericValue = parseFloat(this.value.replace(/[^\d]/g, ''));
  //检查值是否有效
  if (!isNaN(numericValue)) {
    //将数值格式化为货币
    var formattedValue = formatRupiah(numericValue);
    //使用格式化后的值更新输入字段
    $(this).val(formattedValue);
  }
  else{
    //如果值不是数字,则清除输入字段
    $(this).val('');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="form-textbox number-only currency" type="text" id="nilai1" required/>
<input class="form-textbox number-only currency" type="text" id="nilai2" required/>
英文:

You can use this to refer to the specific input field that triggered the event like the following way:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function formatRupiah(nominal){
  return new Intl.NumberFormat(&#39;id-ID&#39;,
    { style: &#39;currency&#39;, currency: &#39;IDR&#39;, minimumFractionDigits: 0 }
  ).format(nominal);
}

$(&#39;.currency&#39;).keyup(function(){
  //get only the number
  var numericValue = parseFloat(this.value.replace(/[^\d]/g, &#39;&#39;));
  //check if the value is valid
  if (!isNaN(numericValue)) {
    //format the numeric value as currency
    var formattedValue = formatRupiah(numericValue);
    //update the input field with the formatted value
    $(this).val(formattedValue);
  }
  else{
    //clear the input field if the value is not numreic
    $(this).val(&#39;&#39;);
  }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input class=&quot;form-textbox number-only currency&quot; type=&quot;text&quot; id=&quot;nilai1&quot; required/&gt;
&lt;input class=&quot;form-textbox number-only currency&quot; type=&quot;text&quot; id=&quot;nilai2&quot; required/&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 17:38:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定