如何在总和之前格式化文本框值? [英] How to format textbox value before sum?
问题描述
我正在循环浏览一些文本框来对值进行求和。如果
中的值,文本框中有一个点或空格为千分隔符,则总和函数
不起作用。
有人可以帮助我让千万分离器工作吗?
功能UpdateBudget(callingcontrol,totalcontrol)
{
var amount = 0
var f = document.forms [''form1''];
if(f){
f = f .elements;
}
var i = f.length;
var disp =""
while(i - > 0){
if(f [i] .type ==''text''){
if(f [i]。 name.indexOf(" Budget")> = 0){
if(f [i] .name.substr(0,16)==
callingcontrol。 name.substr(0,16)){
var n = parseInt(f [i] .value,10);
if(isNaN(n) )){
金额+ = 0;
}
其他{
金额+ = n;
}
}
}
}
}
>
SumBudget(调用控制,金额)
}
感谢您的帮助!
问候,
S
I''m looping through some textboxes to sum the values. If the value in
the textbox has a dot or a space as thousand separator the sum function
doesn''t work.
Can someone help me to make it work also with thousand separators?
function UpdateBudget(callingcontrol, totalcontrol)
{
var amount = 0
var f = document.forms[''form1''];
if (f) {
f = f.elements;
}
var i = f.length;
var disp = ""
while (i-- > 0) {
if (f[i].type == ''text'') {
if (f[i].name.indexOf("Budget") >= 0) {
if (f[i].name.substr(0,16) ==
callingcontrol.name.substr(0,16)) {
var n = parseInt(f[i].value, 10);
if (isNaN(n)) {
amount += 0;
}
else {
amount += n;
}
}
}
}
}
SumBudget(callingcontrol, amount)
}
Grateful for help!
Regards,
S
推荐答案
st ** **@gmail.com 在3/7/2006 1:32 AM说了以下内容:
st****@gmail.com said the following on 3/7/2006 1:32 AM:
我正在循环浏览一些文本框来对值进行求和。如果文本框中的值有一个点或空格为千分隔符,则和函数
不起作用。
逗号分隔符怎么样?
有人可以帮助我使用千位分隔符吗?
< snip>
var n = parseInt(f [i] .value,10);
I''m looping through some textboxes to sum the values. If the value in
the textbox has a dot or a space as thousand separator the sum function
doesn''t work.
What about a comma separator?
Can someone help me to make it work also with thousand separators?
<snip>
var n = parseInt(f[i].value, 10);
>
var n = + f [i] .value.replace('''','''')。replace(''''','''')。replace('',''' ,'''';
将删除任何空格,逗号或句点(点)。让我想知道
如果它有一个小数部分就会发生......虽然......
-
兰迪
comp.lang.javascript常见问题 - http://jibbering.com/常见问题&新闻组每周
Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /
var n = +f[i].value.replace('' '','''').replace(''.'','''').replace('','','''');
Will remove any spaces, commas or periods (dot). Makes me wonder what
happens if it has a decimal part to it though......
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Randy Webb写道:
Randy Webb wrote:
var n = + f [i] .value.replace(' ''','''')。replace(''''','''')。replace('','','''')
将删除任何空格,逗号或句号(点)。让我想知道如果它有一个小数部分会发生什么......虽然......
var n = +f[i].value.replace('' '','''').replace(''.'','''').replace('','','''');
Will remove any spaces, commas or periods (dot). Makes me wonder what
happens if it has a decimal part to it though......
var s =''.. ,, xx '';
alert(s.replace('''','''')。replace(''''','''')。replace('' ,'',''''));
alert(+ s.replace('''','''')。replace(''''','''') .replace('','','''');
我完全相信你知道替换不是单一的递归
字符串,但是因为你喜欢在人员代码上找到这样的愚蠢错误,
我也展示了你的那个,但我不会再这样做了,因为这真是一个
白痴行为,让我记得我们的朋友Spock先生:b
-
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Jonas Raoni在3/7/2006凌晨2:31发表以下文章:
Jonas Raoni said the following on 3/7/2006 2:31 AM:
Randy Webb写道:
Randy Webb wrote:
var n = + f [i] .value.replace('''','''')。replace(''。'', '''').replace('','','''');
将删除任何空格,逗号或句点(点)。让我想知道如果它有一个小数部分会发生什么......虽然......
var n = +f[i].value.replace('' '','''').replace(''.'','''').replace('','','''');
Will remove any spaces, commas or periods (dot). Makes me wonder what
happens if it has a decimal part to it though......
var s =''.. ,, xx''; <警告(s.replace('''','''')。replace(''''','''')。replace('','',''''' );;
alert(+ s.replace('''','''')。replace(''''','''')。replace('','','''' '));
我完全相信你知道替换不是单个
字符串的递归,但是因为你喜欢在人员代码上找到这样的愚蠢错误,
我也展示了你的那个,但我不会再这样做,因为这是一个真正的白痴行为,让我记住我们的朋友Spock先生:b
var s = ''.. ,, xx'';
alert(s.replace('' '','''').replace(''.'','''').replace('','',''''));
alert(+s.replace('' '','''').replace(''.'','''').replace('','',''''));
I''m totally sure you know that the replace isn''t recursive with single
strings, but as you love to find such idiot mistakes on the people code,
I showed your one too, but I won''t do it again, since this is a really
idiot behaviour, makes me remember of our friend Mr. Spock :b
>
我不是在追捕愚蠢的错误,但是当我看到它们时,我确实指出了它们。
是的,你是对的。
当我指出它时,我做*做的一件事是提供解决方案,当
更正时我接受它:
var n = + f [i] .s.replace(/\。/ g,'''')。renplace(/ \,/ g,'''').replace( / \ / g,'''');
但你已经知道了,对吧?
-
Randy
comp.lang.javascript常见问题 - http:// jibbering.com/faq &新闻组每周
Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /
I don''t hunt "idiot mistakes", but I do point them out when I see them.
And yes, you are correct.
One thing I *do* do when I point it out is to offer a solution, and when
corrected I accept it:
var n= +f[i].s.replace(/\./g, '''').replace(/\,/g, '''').replace(/\ /g, '''');
But you already knew that, right?
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
这篇关于如何在总和之前格式化文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!