[英]AutoNumeric change value
I'm trying to make my value to have thousand separators and such, but I don't understand how to use jquery and how it works.我试图使我的价值具有千位分隔符等,但我不明白如何使用 jquery 以及它是如何工作的。
I want to make my value from 1000 to 1,000我想让我的价值从 1000 到 1,000
and I tried using AutoNumeric like this but failed我尝试像这样使用 AutoNumeric 但失败了
var autonumeric = new AutoNumeric.multiple(".form-control");
This is the form where I want to change the value type:这是我要更改值类型的表单:
<div class="form-group">
<div class="row">
<div class="col-lg-3">
<label for="total" class="label-control" id="labelsubtotal">Total : </label>
</div>
<div class="col-lg-9">
<input type="text" value="" style="text-align: right;" class="form-control" id="grandTotal" name="grandTotal" disabled>
</div>
</div>
</div>
This is how I import AutoNumeric:这就是我导入 AutoNumeric 的方式:
<script src="<?php echo base_url();?>js/autonumeric-next/src/AutoNumeric.js" type="text/javascript"></script>
Basically this is what I want to do :基本上这就是我想要做的:
I want the result format changed into currency format我希望将结果格式更改为货币格式
I succeeded in changing only 1 value, this is what I did:我仅成功更改了 1 个值,这就是我所做的:
<script>
$('document').ready(function(){
$(function sum() {
console.log($('.calc'))
var sum = 0.0;
$('.calc').each(function() {
sum += parseInt($(this).text());
});
$("#subTotal").val(sum);
let subTotal = new AutoNumeric("#subTotal");
})();
function calculateSubTotal() {
var subtotal = $("#subTotal").val();
$("#subTotalDiscount").val(subtotal - (Math.round(($("#inputDiscount").val() / 100) * subtotal)));
var subtotal_discount = parseInt($("#subTotalDiscount").val());
$("#subTotalTax").val(Math.round(($("#inputTax").val() / 100) * subtotal_discount));
var subtotal_tax = parseInt($("#subTotalTax").val());
var pph = $("#inputpph").val();
$("#SubTotalpph").val(Math.round(parseInt($("#inputpph").val()*subtotal_discount)));
var subtotal_pph = parseInt($("#SubTotalpph").val());
var grandtotal = subtotal_discount + subtotal_tax + subtotal_pph;
$("#grandTotal").val(grandtotal);
}
})
</script>
I'm lost right now.我现在迷路了。
Also exists a little bit another way to do it.也存在一些另一种方式来做到这一点。 Do next and look what you'll get(one requirement - you need to give number value only):接下来做,看看你会得到什么(一个要求 - 你只需要给出数值):
JS JS
$('#grandTotal').on('input',function(){
var number, s_number, f_number;
number = $('#grandTotal').val();
s_number = number.replace(/,/g,'');
f_number = formatNumber(s_number);
console.info(f_number);
$('#grandTotal').val(f_number);
});
function formatNumber(num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}
But, if you want just convert value without changing it dynamically then just add this in your attached *.js
:但是,如果您只想转换值而不动态更改它,那么只需在附加的*.js
添加它:
var number, f_number;
number = $('#grandTotal').val();
f_number = formatNumber(number);
console.info(f_number);
$('#grandTotal').val(f_number);
function formatNumber(num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.