How can I remove AutoNumeric formatting before submitting form?

后端 未结 11 3017
栀梦
栀梦 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条回答
  • 2021-02-19 06:18

    Solution for AJAX Use Case

    I believe this is better answer among all of those mentioned above, as the person who wrote the question is doing AJAX. So kindly upvote it, so that people find it easily. For non-ajax form submission, answer given by @jpaoletti is the right one.

    // Get a reference to any one of the AutoNumeric element on the form to be submitted
    var element = AutoNumeric.getAutoNumericElement('#modifyQuantity');
    
    // Unformat ALL elements belonging to the form that includes above element
    // Note: Do not perform following in AJAX beforeSend event, it will not work
    element.formUnformat();
    
    $.ajax({
        url: "<url>",
        data : {
            ids : ids,
            orderType : $('#modifyOrderType').val(),
            
            // Directly use val() for all AutoNumeric fields (they will be unformatted now)
            quantity : $('#modifyQuantity').val(),
            price : $('#modifyPrice').val(),
            triggerPrice : $('#modifyTriggerPrice').val()
        }
    })
    .always(function(  ) {
        // When AJAX is finished, re-apply formatting    
        element.formReformat();     
    });
    
    0 讨论(0)
  • 2021-02-19 06:19

    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.

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

    With newer versions you can use the option:

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

    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'));
    });
    
    0 讨论(0)
  • 2021-02-19 06:22
    $("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 ^_^

    0 讨论(0)
提交回复
热议问题