//輸入的時候若是輸入小數2.220當即變成2.22保留兩位js工具類
function clearNoNum(obj){
obj.value = obj.value.replace(/[^\d.]/g,"");
obj.value = obj.value.replace(/\.{2,}/g,".");
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
if(obj.value.indexOf(".")< 0 && obj.value !=""){
obj.value= parseFloat(obj.value);
}
}
//總費用合計計算
$(function(){
$("input:lang(background)").css({"background-color":"#d7d7d7","color":"back"})
$("input:lang(bc)").css({"background-color":"#d7d7d7","color":"back"})
var fyhjinput = document.querySelectorAll('.fyhj');
$.each($(".fyhj"),function(i,n){
var input=fyhjinput[i];
input.onkeyup= function () {
clearNoNum(this)
var hjfy=0;
for(var i=0;i<fyhjinput.length;i++){
var inpVal=parseFloat($(fyhjinput[i]).val()==null||$(fyhjinput[i]).val()==""?0:$(fyhjinput[i]).val());
hjfy+=inpVal;
}
$("#fyhj").val(hjfy.toFixed(2))
}
})
})css
<div class="layui-inline">
<label class="layui-form-label">加油金額(元)</label>
<div class="layui-input-block">
<input type="text" maxlength="12" lay-verify="number" class="layui-input fyhj" name="jyje" placeholder="加油金額(元)" autocomplete="off" />
</div>
</div>工具
<div class="layui-inline">
<label class="layui-form-label">路橋費(元)</label>
<div class="layui-input-block">
<input type="text" maxlength="12" lay-verify="number" class="layui-input fyhj" name="lqf" placeholder="路橋費(元)" autocomplete="off" />
</div>
</div>ui
<div class="layui-inline">
<label class="layui-form-label">維修保養費(元)</label>
<div class="layui-input-block">
<input type="text" maxlength="12" lay-verify="number" class="layui-input fyhj" name="wxbyf" placeholder="維修保養費(元)" autocomplete="off" />
</div>
</div>this
<div class="layui-inline">
<label class="layui-form-label">其餘費用(元)</label>
<div class="layui-input-block">
<input type="text" maxlength="12" lay-verify="number" class="layui-input fyhj" name="qtfy" placeholder="其餘費用(元)" autocomplete="off" />
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">合計費用(元)</label>
<div class="layui-input-block">
<input type="text" maxlength="12" lang="background" id="fyhj" value="0.00" readonly="readonly" lay-verify="number" class="layui-input" name="hjfy" placeholder="合計費用(元)" autocomplete="off" />
</div>
</div>orm