格式化输入文本字段的新数据

时间:2011-12-15 14:57:15

标签: jquery forms

    // capture selling price change 
$('#frmFilters').on('change', 'input[name="sell[]"]', function() {

    // define the tr that we want to pull form values from
    var row = $(this).closest('tr');

    // set up values for form fields to pass through to ajax call
    var sell = row.find('.sell').val();

    var pkID = row.find('.lineup').val();

    var sku = (row.find('.sku').map(function(){
        return this.name + '=' + this.value;
    }).get().join('&'));

    // do ajax call
    $.ajax({ ... });

})

从上面的代码我想格式化输入到文本字段名称= sell []的价格,其中还有一个卖出类别总是有2个小数点

我怎样才能实现这一目标

1 个答案:

答案 0 :(得分:1)

你可以使用javascript函数toFixed(2),这样它总是有两个小数

$('#frmFilters').on('change', 'input[name="sell[]"]', function() {

   var numericVal = parseInt(this.value, 10);
   numericVal = numericVal.toFixed(2);
   //set the value of the input field to the formatted value
    this.value=numericVal;
})

编辑 - 这里有多个字段http://jsfiddle.net/LtR9N/2/

相关问题