在jQuery的“实时"输入字段中删除逗号 [英] Removing commas in 'live' input fields in jquery
问题描述
我在表中有一系列输入框,其中包含一些动态生成的行,例如:
I have a series of input boxes in a table with some number of dynamically generated rows as such:
<table id="someDataTable">
<tbody>
<tr>
<td >Some Title</td>
<td >Units</td>
<td >Val/Unit</td>
<td >Value</td>
</tr>
<tr>
<td><input type="text" size="30" /></td>
<td><input type="text" size="14" class="units commas"/></td>
<td><input type="text" size="14" class="value commas"/></td>
<td><input type="text" size="14" readonly="readonly" class="autoTotal"/></td>
</tr>
...
</tbody>
</table>
现在,我有一个blur()调用,用于在每次退出输入框时添加逗号,以使用漂亮的数字格式化程序插件,它可以做到这一点:
Now, I have a blur() call to add commas every time an input box is exited to add commas with the nifty Number Formatter plugin, and it simply does this:
<script>
$(".commas").blur(function () {
$(this).parseNumber({ format: "#,###", locale: "us" });
$(this).formatNumber({ format: "#,###", locale: "us" });
});
</script>
而且效果很好.现在,另一方面,我还有一大堆代码,可以在每次击键时自动进行形式数学运算.它在initialize()中有一个调用,如下所示:
And it works beautifully. Now, on the other side, I also have a chunk of code that does the form math automatically on every keystroke. It has a call in initialize() that looks like this:
$(document).on('keyup', '#someDataTable', DoCalculations);
它调用的函数如下:
function DoCalculations() {
$(this).find('tr').each(function () {
var tUnits = $(this).find('.units').val();
var tValue = $(this).find('.value').val();
$(this).find('.autoTotal').val(Math.round(tUnits * tValue));
});
}
-
现在,我的问题是:我需要能够删除逗号来进行计算.我希望能够使用NumberFormatter的parseNumber()函数来做到这一点,但是它有点小.这是DoCalculations中尝试实现此目标的备用代码:
Now, my problem: I need to be able to rip out the commas to do the calculations. I was hoping to be able to use NumberFormatter's parseNumber() function to do this, but it was having a small fit. This was the alternate code in DoCalculations to attempt to accomplish that:
function DoCalculations() {
$(this).find('tr').each(function () {
var tTotal;
var tUnits = $(this).find('.units').val();
var tValue = $(this).find('.value').val();
tUnits = $.parseNumber(tUnits, { format: "#,###", locale: "us" });
tValue = $.parseNumber(tValue, { format: "#,###", locale: "us" });
tTotal = tUnits * tValue;
tTotal = $.formatNumber(tTotal, { format: "#,###", locale: "us" });
$(this).find('.autoTotal').val(tTotal);
});
}
但是它返回NumberFormatter.js中的运行时错误,在该错误中无法获取未定义或null引用的numberString.indexOf属性.确切地说,是第442行.我不确定为什么.我原本以为是因为输入框是空的,但这没关系.
But it comes back with a runtime error in NumberFormatter.js, where it cannot get the property of numberString.indexOf of undefined or null reference. Line 442 to be exact. I'm not sure why though. I originally thought it was because there were empty input boxes, but that turned out to not matter.
最后,我需要去除逗号.
At the end of the day, I need to strip out commas.
推荐答案
正则表达式擅长于删除字符:
Removing characters is something that regular expressions excel at:
var tValue = parseFloat($(this).find('.value').val().replace(/,/g, ''));
更新
如果val()
可以为null/undefined,则可以添加如下检查:
If val()
can be null/undefined, you can add a check like this:
var tValue = $(this).find('.value').val();
tValue = tValue ? parseFloat(tValue.replace(/,/g, '')) : 0;
这篇关于在jQuery的“实时"输入字段中删除逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!