0

I am attempting to make the number entered auto shift to 2 decimal places - but after several attempts I can't get this to go - and ideas?:

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));
    });

Edit: To take a look at my problem more closely - I created a fiddle: http://jsfiddle.net/drewwyatt/AEWHs/

4

2 に答える 2

3

Use this.value instead of $(this).value:

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

Or $(this).val() if you like it more jQuery'ish.

Update

The further problem sits on the input value type. You need to parse its value as integer via parseFloat, parseInt or else...

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

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

于 2013-03-24T22:58:23.073 に答える
1

I was able to get this working using

<tr>
    <td colspan="2" class="null"></td>
    <td class="label">Money Accounted For</td>
    <td class="field"><input type="text" name="moneyAccountedFor" id="moneyAccountedFor" onblur ="this.value = Number(this.value).toFixed(2)"></td>
</tr>

Fiddle here:

http://jsfiddle.net/drewwyatt/AEWHs/1/

于 2013-03-24T23:47:16.523 に答える