gpt4 book ai didi

javascript - toFixed onBlur 不想开火?

转载 作者:行者123 更新时间:2023-11-28 20:35:00 24 4
gpt4 key购买 nike

我试图让输入的数字自动转移到小数点后两位 - 但经过几次尝试后我无法实现 - 有什么想法吗?:

html

<tr>
<td colspan="2" class="null"></td>
<td class="label">Money Accounted For</td>
<td class="field"><input type="text" name="moneyAccountedFor" id="moneyAccountedFor" tabindex="7" /></td>
</tr>

JQuery

$("#moneyAccountedFor").blur(function() {
$(this).val($(this).value.toFixed(2));
});

编辑:为了更仔细地看看我的问题 - 我创建了一个 fiddle :http://jsfiddle.net/drewwyatt/AEWHs/

最佳答案

使用<b>this</b>.value而不是<b>$(this)</b>.value :

$("#moneyAccountedFor").blur(function() {
this.value = this.value.toFixed(2);
});

或者$(this)<b>.val()</b>如果你更喜欢 jQuery 风格。

更新

进一步的问题在于输入值类型。您需要通过 parseFloat 将其值解析为整数, parseInt else ...

$("#moneyAccountedFor").blur(function() {
this.value = parseInt(this.value).toFixed(2);
});

http://jsfiddle.net/AEWHs/2/

关于javascript - toFixed onBlur 不想开火?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15605111/

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