【问题标题】:How can I remove AutoNumeric formatting before submitting form?如何在提交表单之前删除 AutoNumeric 格式?
【发布时间】:2013-04-04 15:43:01
【问题描述】:

我正在使用 jQuery 插件 AutoNumeric,但是当我提交表单时,我无法删除 POST 之前字段的格式。

我尝试使用$('input').autonumeric('destroy')(和其他方法),但它在文本字段中留下了格式。

我如何POST将未格式化的数据发送到服务器?如何删除格式?在初始配置或其他地方是否有它的属性?

我不想将序列化的表单数据发送到服务器(使用 AJAX)。我想提交带有未格式化数据的表单,就像普通的 HTML 操作一样。

【问题讨论】:

    标签: javascript jquery jquery-plugins autonumeric.js


    【解决方案1】:

    我在 jQuery 中为此编写了一个更好、更通用的 hack

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

    此代码使用非 autoNumeric 值的错误处理清除表单。

    【讨论】:

    • 很好,但是一两秒钟(在提交表单时)我看到了未格式化的值。这只是一个外观问题,但有点烦人。
    • 不再需要这个了,您现在可以使用以下选项:{ unformatOnSubmit : true }
    【解决方案2】:

    对于较新的版本,您可以使用以下选项:

    unformatOnSubmit: true
    

    【讨论】:

    【解决方案3】:

    在数据回调中,您必须调用 getString 方法,如下所示:

            $("#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) {
    
                        }
                    }
                }
            }
        });
    

    【讨论】:

      【解决方案4】:

      使用get 方法。

      '得到' |通过“.val()”返回未格式化的对象或 ".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>
      

      这将始终提交"15"。现在发挥创意:)


      镜像原始值:

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

      submit.php 中的忽略 my_field_formatted 的值并改用 my_field

      【讨论】:

      • get 方法返回输入字段的值。我需要提交未格式化的表格。我不想获取字段的未格式化值。
      • 然后get 值并提交?我假设您有一些自定义的 onsubmit 处理程序,如果没有,请制作一个,您将需要它。
      • 所以没有ajax请求就无法提交无格式数据?
      • 当然可以。您可以修改 onsubmit 处理程序中的字段值。它不会像 AJAX 那样干净整洁,但它肯定是可行的。
      • 如果你能给我这个解决方案,我一定会接受你的回答。 :)
      【解决方案5】:

      你可以随时使用phpstr_replace函数

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

      它将删除所有逗号。如有必要,您可以将值转换为整数。

      【讨论】:

      • 为什么是 php? (仅供参考,我在应用程序的服务器端使用了 java。)错误的解决方案,不是答案。
      • 没关系。 Java 可能有具有类似功能的方法。我认为它是一种解决方案,因为它会删除格式,这是我们想要的。为什么在客户端做它很重要?
      • 因为它在多层应用程序中更干净。阅读即en.wikipedia.org/wiki/Loose_coupling 的想法。
      【解决方案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 是您的输入类,它将作为 autoNumeric 初始化 对不起英语不好^_^

      【讨论】:

        【解决方案7】:

        还有另一种集成解决方案,它不会干扰您的客户端验证,也不会导致提交前未格式化的文本闪烁:

        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】:

          您可以使用 getArray 方法 (http://www.decorplanit.com/plugin/#getArrayAnchor)。

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

          【讨论】:

            【解决方案9】:

            我想出了这个,似乎是最干净的方法。 我知道这是一个很老的帖子,但它是第一个 Google 匹配,所以我会把它留在这里以备不时之需

            $('form').on('submit', function(){
                $('.curr').each(function(){
                    $(this).autoNumeric('update', {aSign: '', aDec: '.', aSep: ''});;
                });
            });
            

            【讨论】:

              【解决方案10】:

              AJAX 用例解决方案

              我相信这是上述所有问题中更好的答案,因为写这个问题的人正在做 AJAX。所以 请点赞,以便人们轻松找到它。对于非 ajax 表单提交,@jpaoletti 给出的答案是正确的。

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

              【讨论】:

                【解决方案11】:

                autoNumeric("getArray") 不再有效。

                unformatOnSubmit: true 在使用 serializeArray() 使用 Ajax 提交表单时似乎不起作用。

                改为使用formArrayFormatted获取form.serializeArray()等价的序列化数据

                只需从表单中获取任何 AutoNumeric 初始化元素并调用该方法。它将序列化整个表单,包括非自动数字输入。

                $.ajax({
                    type: "POST",
                    url: url,
                    data: AutoNumeric.getAutoNumericElement("#anyElement").formArrayFormatted(),
                )};
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2011-08-19
                  • 2015-12-01
                  • 1970-01-01
                  • 2020-06-19
                  • 2011-01-25
                  • 2016-10-03
                  • 2015-02-23
                  • 1970-01-01
                  相关资源
                  最近更新 更多