gpt4 book ai didi

javascript - 自动计算并转换为货币格式 Javascript

转载 作者:行者123 更新时间:2023-12-03 05:29:59 25 4
gpt4 key购买 nike

我想再问一次..希望你知道我想自动计算,但使用格式钱..

这个演示:https://jsfiddle.net/xp4ky2gg/

这是我的代码...

html代码

                    <table style="width:100%">
<thead>
<tr>
<th>Pendapatan Keluarga (A) Bersumber dari</th>
<th>Jumlah (Rp/Bulan)</th>
</tr>
</thead>
<tbody>
<tr>
<td>Usaha Pokok Suami</td>
<td>
<input type="text" name="pendapatan_suami" id="nominal1" class="pendapatan" style="width: 200px; text-align:right;"onkeyup="angka(this)" >
</td>
</tr>
<tr>
<td>Usaha Pokok Istri</td>
<td>
<input type="text" name="pendapatan_istri" id="nominal2" class="pendapatan" style="width: 200px; text-align:right;" onkeyup="angka(this)" >
</td>
</tr>
<tr>
<td>Usaha Lainnya</td>
<td>
<input type="text" name="pendapatan_usaha" id="nominal3" class="pendapatan" style="width: 200px; text-align:right;" onkeyup="angka(this)">
</td>
</tr>
<tr>
<td>Dari orangtua</td>
<td>
<input type="text" name="pendapatan_ortu" id="nominal3" class="pendapatan" style="width: 200px; text-align:right;" onkeyup="angka(this)">
</td>
</tr>
<tr>
<td>Dari anak/menantu</td>
<td>
<input type="text" name="pendapatan_anak" id="nominal4" class="pendapatan" style="width: 200px; text-align:right;" onkeyup="angka(this)">
</td>
</tr>
<tr>
<td>Penghasilan Lainnya sebutkan</td>
<td><input type="text" name="pendapatan_lain" id="nominal6" class="pendapatan" style="width: 200px; text-align:right;" onkeyup="angka(this)"></td>
</tr>
<tr>
<td>Total</td>
<td>
<input type="text" name="total" id="total" style="width: 200px;">
</td>
</tr>
<tr>
<td>Kesimpulan III<span class="red">*</span></td>
<td>
<select id="k3" class="tes3" name="kelayakan3" size="1" >
<option value="">-- Pilih Salah Satu --</option>
<option value="1">Layak</option>
<option value="0">Tidak Layak</option>
</select>
</td>
</tr>
<tr>
<td style="vertical-align:top;" class="wrap">Ket Kelayakan III</td>
<td><textarea name="ketkel3" maxlength="100"></textarea></td>
</tr>
</tbody>
</table>

JavaScript 代码

      function NilaiRupiah(jumlah){  
var titik = ".";
var nilai = new String(jumlah);
var pecah = [];
while(nilai.length > 3)
{
var asd = nilai.substr(nilai.length-3);
pecah.unshift(asd);
nilai = nilai.substr(0, nilai.length-3);
}

if(nilai.length > 0) { pecah.unshift(nilai); }
nilai = pecah.join(titik);
return nilai;
}

function angka(e) {
if (!/^[0-9]+$/.test(e.value)) {
e.value = e.value.substring(0,e.value.length-1);
}
}

$(document).ready(function () {
//Iterate through each Textbox and add keyup event handler
$(".pendapatan").each(function () {
$(this).keyup(function () {
//Initialize total to 0
var total = 0;
$(".pendapatan").each(function () {
// Sum only if the text entered is number and greater than 0
if (!isNaN(this.value) && this.value.length != 0) {
total += parseFloat(this.value);
$(this).css("background-color", "#FEFFB0");
}
else if (this.value.length != 0){
$(this).css("background-color", "red");
}
});
//Assign the total to label
//.toFixed() method will roundoff the final sum to 2 decimal places
jml = "Rp. " + NilaiRupiah(total) + ",00";
$("#total").val(jml);
});
});
});

这个演示:https://jsfiddle.net/xp4ky2gg/如果计算

立即输出

  9000
9000
-------
18.000

我想像这样输入和输出 9000 转换为 9.000,结果为 18.000

  9.000
9.000
---------
18.0000

最佳答案

也许你的意思是装饰(数字格式)值,但仍然保留计算,像这样

      $('.pendapatan').focus(function(){
var v = $(this).val().replace(/[^0-9]/, '');
$(this).val(v);
}).blur(function(){
var v = NilaiRupiah($(this).val());
$(this).val(v);
})

https://jsfiddle.net/gn0a2h8h/

关于javascript - 自动计算并转换为货币格式 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40987844/

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