问题描述
我在算术上操作表单元素值时遇到了困难。
我已将数据输入到表单中,然后使用js获取它们,如下所示:
p7Left = Number(document.form1.elements [" p7_left"]。value);
p7Right = Number(document.form1.elements [" p7_right" ] .value);
...
...
scoreLeft = Number(document.form1.elements [" left_score"] .value);
scoreRight = Number(document.form1.elements [" right_score"]。value);
然后我按如下方式添加:
scoreLeft =数字(scoreLeft + p& Left + ...... +);
这就是我想要的;没有操作员''Number''我得到了各种变量的连接价格(如预期的那样)。有没有某种方式全局
将所有变量定义为数字而不是字符串?
-
Ed Jay(删除M通过电子邮件回复)
I''m having difficulties arithmetically manipulating form element values.
I''ve entered data into the form, and I fetch them using a js, as:
p7Left = Number(document.form1.elements["p7_left"].value);
p7Right = Number(document.form1.elements["p7_right"].value);
...
...
scoreLeft = Number(document.form1.elements["left_score"].value);
scoreRight = Number(document.form1.elements["right_score"].value);
Then I add these as follows:
scoreLeft = Number(scoreLeft + p&Left + ...... +);
This does what I want; without the operator ''Number'' I get a concatination
of the various variables (as expected). Is there some way of globally
defining all variables as numbers instead of strings?
--
Ed Jay (remove M to respond by email)
推荐答案
No.
但是您应该看到
关于这个主题的条目以及解决它的最佳方法。
-
Matt Kruse
No.
But you should see http://www.JavascriptToolbox.com/bestpractices/ for an
entry about this topic and the best way to solve it.
--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
但你应该看到有关此主题的
条目以及解决它的最佳方法。
No.
But you should see http://www.JavascriptToolbox.com/bestpractices/ for an
entry about this topic and the best way to solve it.
谢谢。
-
Ed Jay (删除M以通过电子邮件回复)
Thanks.
--
Ed Jay (remove M to respond by email)
No.
在当前版本的在浏览器中使用的Javascript,变量没有
类型。只有值具有类型。如果你把一个字符串放到一个变量中,那么
也是你得到的。
你在将表单控件值转换为
数字。然后你应该记住你的值包含的类型。
从第一个变量中添加数字的结果已经是
a数,所以你不需要转换它再次分手之前
得分左右。
祝你好运
/ L
-
Lasse Reichstein Nielsen -
DHTML死亡颜色:< URL :http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''没有判断的信仰只会降低精神神圣。''
No.
In the current version of Javascript used in browsers, variables have no
type. Only values have type. If you put a string into a variable, that
is also what you get out.
You are doing the right thing in converting form control values to
numbers. You should then remember what types your values contain.
The result of adding the numbers from the first variables is already
a number, so you don''t need to convert it again before assingning to
scoreLeft.
Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''
这篇关于字符串和数字......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!