gpt4 book ai didi

javascript - 使用 jquery 或 javascript 将逗号添加到禁用字段

转载 作者:行者123 更新时间:2023-11-30 05:45:08 25 4
gpt4 key购买 nike

我有一个用于计算一些数字的表单,表单上的最后 3 个输入字段被禁用,因为它们显示计算器的结果。

我正在使用以下 javascript/jquery 将逗号添加到用户可编辑字段中,效果很好,但我似乎无法找到一种方法将逗号添加到“结果”字段中:

$('input.seperator').change(function(event){
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
var $this = $(this);
var num = $this.val().replace(/,/gi, "").split("").reverse().join("");

var num2 = RemoveRougeChar(num.replace(/(.{3})/g,"$1,").split("").reverse().join(""));

// the following line has been simplified. Revision history contains original.
$this.val(num2);
});

function RemoveRougeChar(convertString){

if(convertString.substring(0,1) == ","){

return convertString.substring(1, convertString.length)

}

return convertString;
}

这就是我用来填充字段的内容,基本上字段以美元显示结果,所以我尝试每 3 个数字添加一个逗号:

$('#incorrect-payment').val(fieldK);
$('#correcting-payment').val(fieldL);
$('#total-cost').val(fieldM);

最佳答案

我想你会想使用这样的函数:

function FormatCurrency(amount, showDecimals) {

if (showDecimals == null)
showDecimals = true;

var i = parseFloat(amount);
if (isNaN(i)) { i = 0.00; }
var minus = false;
if (i < 0) { minus = true; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);

if (showDecimals) {
if (s.indexOf('.') < 0) { s += '.00'; }
if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
}

//s = minus + s;
s = '$' + FormatCommas(s, showDecimals);
if (minus)
s = "(" + s + ")";
return s;
}

function FormatCommas(amount, showDecimals) {
if (showDecimals == null)
showDecimals = true;


var delimiter = ","; // replace comma if desired
var a = amount.split('.', 2)
var d = a[1];
var i = parseInt(a[0]);
if (isNaN(i)) { return ''; }
var minus = '';
if (i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while (n.length > 3) {
var nn = n.substr(n.length - 3);
a.unshift(nn);
n = n.substr(0, n.length - 3);
}
if (n.length > 0) { a.unshift(n); }
n = a.join(delimiter);

if (!showDecimals) {
amount = n;
}
else {
if (d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
}
amount = minus + amount;
return amount;
}

关于javascript - 使用 jquery 或 javascript 将逗号添加到禁用字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18365476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com