【问题标题】:JqueryUI spinner numberformat currency submit number valueJqueryUI spinner numberformat 货币提交数字值
【发布时间】:2014-09-09 19:22:24
【问题描述】:

我有一个 jquery 货币输入,我正在使用这个代码:

$('input[name="precio"]').spinner({
    min:0,
    numberFormat: 'C'
});

输入工作完美,因为它根据全球化将数字显示为带有 $ 或 € 符号的数字。

问题是,当我提交表单时,我得到的文本是“5,00 €”,我希望它是 5.00 或 500,只是数值。

有没有办法做到这一点?提前致谢。

【问题讨论】:

    标签: jquery jquery-ui-spinner jquery-globalization


    【解决方案1】:

    这是一个名为“aria-valuenow”的属性,用于获取当前数值(无格式)的输入

    http://jsfiddle.net/ma8zd7mg/1/

     $("#spinner").spinner({
         min: 0,
         numberFormat: "C"
     });
    
    $("#test").click(function(){
        alert($("#spinner").attr('aria-valuenow'));
    });
    

    EDIT(在 cmets 中回答您的问题) 要在 PHP 中提交后获取值,您可以: 1. 创建一个占位符输入,获取微调器的数值,然后提交该值 2. 使用 PHP 去除多余的格式字符($、. 和多余的零)

    我推荐第一种方法,类似这样:

    $("#submit-btn").click(function(e){
        //prevent the form submission
        e.preventDefault();
    
        //add a placeholder input with the value of the spinner
        $("#my-from").append('<input/>', {
            'type': 'hidden',
            'name': 'spinner-value',
            'value': $("#spinner").attr('aria-valuenow')
        });
    
        //now submit the form
        $("#my-form").submit();
    });
    

    然后在 PHP 中你可以得到 'spinner-value' 输入

    <?php
    $spinner = $_REQUEST['spinner-value'];
    ....
    ....
    ?>
    

    【讨论】:

    • 谢谢!!但我想在提交后使用 php 中的值,是否有必要在提交之前将输入转换为 ariavalue,或者有没有办法为每个带有 numberFormat C 的微调器自动执行此操作(如插件或其他东西)?
    • 我更新了答案,如果对你有帮助,请标记为答案。
    • 非常感谢,我想我会这样做。我希望它存在某种微调器的扩展,它可以将 aria 值存储在与初始对象具有相同名称和 id 的隐藏输入中,同时在其他可见输入中向用户显示格式化数字。像 datepicker altfield 和 altformat。
    【解决方案2】:

    好吧,我终于按照我想要的方式做到了。我刚刚创建了一个名为 currencyspinner 的插件,它从 JQueryUI 扩展了 spinner,代码如下:

    $.widget( "ui.currencyspinner", $.ui.spinner, {
         options: {
            numberFormat: 'C',  // Currency Format
            min:0,
            step:0.10
        },
        _create: function(){
            this.hidden = $('<input type="hidden" value="'+this.element.val()+'" name="'+this.element.attr('name')+'">');   // Create a hidden input with the same name and value
            this.element.removeAttr('name');    // remove the name of the original element
            this.element.before(this.hidden);   // insert the hidden element before the original element
            return this._super();
        },
    
        _refresh: function() {
            this.hidden.val(this._parse(this.element.val()));   // put the aria-valuenow on the original element
            return this._super();
        },
    
        _destroy: function(){
            this.element.attr('name',this.hidden.attr('name')); // put back the name on the original value
            this.hidden.remove();                               // remove the hidden element
            return this._super();
        }
    });
    

    我创建一个具有相同名称的隐藏输入,并在每次微调器刷新时为其赋予 aria-valuenow 的值。

    如果您在创建货币微调器后尝试访问货币微调器,请务必小心,您不能使用 $('input[name="nameoftheelement"]').currencyspinner(...);,因为这将是隐藏输入,而不是微调器本身。

    我希望它可以帮助某人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-22
      • 2019-05-14
      • 1970-01-01
      • 2023-03-03
      • 2014-05-24
      • 2011-05-20
      • 2014-03-25
      • 2012-01-23
      相关资源
      最近更新 更多