How can I remove AutoNumeric formatting before submitting form?

后端 未结 11 2960
栀梦
栀梦 2021-02-19 05:26

I\'m using the jQuery plugin AutoNumeric but when I submit a form, I can\'t remove the formatting on the fields before POST.

I tried to use $(\'input\

相关标签:
11条回答
  • You can always use php str_replace function

    str_repalce(',','',$stringYouWantToFix);
    

    it will remove all commas. you can cast the value to integer if necessary.

    0 讨论(0)
  • 2021-02-19 06:08

    You could use the getArray method (http://www.decorplanit.com/plugin/#getArrayAnchor).

    $.post("myScript.php", $('#mainFormData').autoNumeric('getArray'));

    0 讨论(0)
  • 2021-02-19 06:08

    I came up with this, seems like the cleanest way. I know it's a pretty old thread but it's the first Google match, so i'll leave it here for future

    $('form').on('submit', function(){
        $('.curr').each(function(){
            $(this).autoNumeric('update', {aSign: '', aDec: '.', aSep: ''});;
        });
    });
    
    0 讨论(0)
  • 2021-02-19 06:14

    Use the get method.

    'get' | returns un-formatted object via ".val()" or ".text()" | $(selector).autoNumeric('get');


    <script type="text/javascript">
        function clean(form) {
            form["my_field"].value = "15";
        }
    </script>
    
    <form method="post" action="submit.php" onsubmit="clean(this)">
        <input type="text" name="my_field">
    </form>
    

    This will always submit "15". Now get creative :)


    Mirrored raw value:

    <form method="post" action="submit.php">
        <input type="text" name="my_field_formatted" id="my_field_formatted">
        <input type="hidden" name="my_field" id="my_field_raw">
    </form>
    
    <script type="text/javascript">
        $("#my_field_formatted").change(function () {
            $("#my_field").val($("#my_field_formatted").autoNumeric("get"));
        });
    </script>
    

    The in submit.php ignore the value for my_field_formatted and use my_field instead.

    0 讨论(0)
  • 2021-02-19 06:16

    Inside data callback you must call getString method like below:

            $("#form").autosave({
            callbacks: {
                data: function (options, $inputs, formData) {
    
                    return $("#form").autoNumeric("getString");
                },
                trigger: {
                    method: "interval",
                    options: {
                        interval: 300000
                    }
                },
                save: {
                    method: "ajax",
                    options: {
                        type: "POST",
                        url: '/Action',
                        success: function (data) {
    
                        }
                    }
                }
            }
        });
    
    0 讨论(0)
  • 2021-02-19 06:17

    autoNumeric("getArray") no longer works.

    unformatOnSubmit: true does not seem to work when form is submitted with Ajax using serializeArray().

    Instead use formArrayFormatted to get the equivalent serialised data of form.serializeArray()

    Just get any AutoNumeric initialised element from the form and call the method. It will serialise the entire form including non-autonumeric inputs.

    $.ajax({
        type: "POST",
        url: url,
        data: AutoNumeric.getAutoNumericElement("#anyElement").formArrayFormatted(),
    )};
    
    0 讨论(0)
提交回复
热议问题