英文:
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
<input class="form-textbox number-only currency" type="text" id="nilai1" required/>
<input class="form-textbox number-only currency" type="text" id="nilai2" required/>
I managed to find a javascript function to change the number format to currency
function formatRupiah(nominal){
return new Intl.NumberFormat('id-ID',
{ style: 'currency', currency: 'IDR', minimumFractionDigits: 0 }
).format(nominal);
}
And with this function below, I am able to show the value in console
$('.rupiah').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('id-ID',
{ style: 'currency', currency: 'IDR', minimumFractionDigits: 0 }
).format(nominal);
}
$('.currency').keyup(function(){
//get only the number
var numericValue = parseFloat(this.value.replace(/[^\d]/g, ''));
//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('');
}
});
<!-- language: lang-html -->
<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/>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论