1 回答
TA贡献1843条经验 获得超7个赞
您需要先解析该值,然后再添加 1。您的代码是连接 1,然后解析。所以0.00变成0.001,当你打印出小数点后两位时,它又变成了0.00。
var ex = "/^[0-9]+\.?[0-9]*$/";
$("#addDollar").on("click", function() {
var valueOut = $("#totalAmountIn");
valueOut.val((parseFloat(valueOut.val()) + 1).toFixed(2));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Total Amount In</h1>
<input type="text" value="0.00" id="totalAmountIn"></input>
<div class="col-md-4">
<button type="button" id="addDollar" class="btn btn-default">
Add Dollar
</button>
</div>
var ex = "/^[0-9]+\.?[0-9]*$/";
$("#addDollar").on("click", function() {
var valueOut = $("#totalAmountIn");
valueOut.val((parseFloat(valueOut.val()) + .25).toFixed(2));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Total Amount In</h1>
<input type="text" value="0.00" id="totalAmountIn"></input>
<div class="col-md-4">
<button type="button" id="addDollar" class="btn btn-default">
Add $0.25
</button>
</div>
- 1 回答
- 0 关注
- 138 浏览
添加回答
举报