How can I remove AutoNumeric formatting before submitting form?

北城以北 提交于 2020-01-01 08:26:09

问题


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').autonumeric('destroy') (and other methods) but it leaves the formatting on the text fields.

How can I POST the unformatted data to the server? How can I remove the formatting? Is there an attribute for it in the initial config, or somewhere else?

I don't want to send the serialized form data to the server (with AJAX). I want to submit the form with the unformatted data like a normal HTML action.


回答1:


I wrote a better, somewhat more general hack for this in jQuery

$('form').submit(function(){
    var form = $(this);
    $('input').each(function(i){
        var self = $(this);
        try{
            var v = self.autoNumeric('get');
            self.autoNumeric('destroy');
            self.val(v);
        }catch(err){
            console.log("Not an autonumeric field: " + self.attr("name"));
        }
    });
    return true;
});

This code cleans form w/ error handling on not autoNumeric values.




回答2:


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) {

                    }
                }
            }
        }
    });



回答3:


With newer versions you can use the option:

unformatOnSubmit: true



回答4:


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.




回答5:


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.




回答6:


$("input.classname").autoNumeric('init',{your_options});
$('form').submit(function(){
   var form=$(this);
   $('form').find('input.classname').each(function(){
       var self=$(this);
       var v = self.autoNumeric('get');
       // self.autoNumeric('destroy');
       self.val(v);
   });
});

classname is your input class that will init as autoNumeric Sorry for bad English ^_^




回答7:


There is another solution for integration which doesn't interfere with your client-side validation nor causes the flash of unformatted text before submission:

var input = $(selector);
var proxy = document.createElement('input');
proxy.type = 'text';
input.parent().prepend(proxy);
proxy = $(proxy);
proxy.autoNumeric('init', options);
proxy.autoNumeric('set', input.val())''
proxy.change(function () {
    input.val(proxy.autoNumeric('get'));
});



回答8:


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

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




回答9:


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


来源:https://stackoverflow.com/questions/15815771/how-can-i-remove-autonumeric-formatting-before-submitting-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!