0

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

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

输入工作完美,因为它将数字显示为带有 $ 或 € 符号的数字,具体取决于 globalize。

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

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

4

2 回答 2

1

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

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

 $("#spinner").spinner({
     min: 0,
     numberFormat: "C"
 });

$("#test").click(function(){
    alert($("#spinner").attr('aria-valuenow'));
});

编辑(在评论中回答您的问题)要在 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'];
....
....
?>
于 2014-09-09T19:34:25.623 回答
0

好的,我最终设法按照我想要的方式做到了这一点。我刚刚创建了一个名为 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(...);因为这将是隐藏的输入,而不是微调器本身。

我希望它可以帮助某人!

于 2014-10-02T11:54:09.010 回答