简体   繁体   中英

Trailing zeroes should come automatically after/before . (point)

I have an input box :

<input autocomplete="off"  class='amount'>

If i type .75 in the input field then 0 should be added automatically before .75 so that it should become 0.75

Also if i type 1.3 then it should become 1.30

I have tried :

echo "<script>
    $('.amount').on('change', function(){
        $(this).val(parseFloat($(this).val()).toFixed(2));
    });
</script>";

Edited : its showing me same value what i have typed , no change happening

<!DOCTYPE html>
<html>
    <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <?php 
    echo "<script>
        $(document).ready(function(){
            $('.amount').on('change', function(){
                $(this).val(parseFloat($(this).val()).toFixed(2));
            });
        });
    </script>";
    ?>
    </head>
    <body>
        <input autocomplete="off"  class='amount'/>
    </body>
</html>

You need to make sure to wrap your event handlers in document.ready to make sure jquery is properly loaded before assigning them. This is why @laaposto 's fiddle works because jquery is already loaded in the fiddle before the event handler is assigned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM