javascript表单编号 [英] javascript form numbers
问题描述
函数 g
< form name =gaussform>
< input name =min
type =number
min =1
value =1>
< input name =max
type =number
min =2
value =10>
< input name =step
type =number
min =1
value =1>
< input onclick =alert_g()
type =submit
value =calculate>
< / form>
< script type =text / javascript>
函数g(min,max,step){
var actualmax = max - ((max - min)%step)
return(min + actualmax)*((1 +((actualmax - min)/ step))/ 2)
}
function alert_g(){
var frm = document.forms [gaussform] $ b $数值
var max = frm [max]。value
var step = frm [step]。value
if(min = = 1){
alert(min is 1)}
if(max == 10){
alert(max is 10)}
if(step = = 1){
alert(step is 1)}
alert(g(min,max,step))
// // // //返回所需的结果
alert(g (1,10,1))}
< / script>
if语句只能让我明白发生了什么!
因此,如果用户输入1,10,1(默认值),结果应该是55.
alert(g(1,10,1))
- > 55
alert(g(min,max,步骤)
- > 550
alert(g(1,100,2))
- > 2500
alert(g(min,max,step)
- > 4975(显然最小,最大,步长== 1,100,2)
函数 g
是正确的,但我不明白发生了什么传递给它的值。
这个表达式...
min + actualmax
...是问题所在。
您可能想要做到这一点......
+ min + + actualmax
...或使用 parseFloat()
, parseInt()
或其他符合您要求的内容将这些字符串转换为实际的 Number
。
jsFiddle 。
对于算术加法,JavaScript的 +
和字符串连接。因为用户输入始终是一个字符串,所以你在做字符串连接。
My first day of Javascript and I am incredibly confused. I am passing numbers returned from a form to a function, but the result is not consistent with what it should be. My testing leaves a lot to be desired, but hopefully the following makes sense.
The function g
calculates the sum of the sequence.
<form name="gaussform">
<input name="min"
type="number"
min="1"
value="1">
<input name="max"
type="number"
min="2"
value="10">
<input name="step"
type="number"
min="1"
value="1">
<input onclick="alert_g()"
type="submit"
value="calculate">
</form>
<script type="text/javascript">
function g(min,max,step) {
var actualmax = max - ((max - min) % step)
return (min + actualmax) * ((1 + ((actualmax - min) / step)) / 2)
}
function alert_g() {
var frm = document.forms["gaussform"]
var min = frm["min"].value
var max = frm["max"].value
var step = frm["step"].value
if (min == 1) {
alert("min is 1")}
if (max == 10) {
alert("max is 10")}
if (step == 1) {
alert("step is 1")}
alert(g(min,max,step))
// below returns the desired result
alert(g(1,10,1))}
</script>
the if statements are only so I can understand what is going on!
So if the user enter 1,10,1 (the default values) the result should be 55.
alert(g(1,10,1))
-> 55
alert(g(min,max,step)
-> 550
alert(g(1,100,2))
-> 2500
alert(g(min,max,step)
-> 4975 (obviously min,max,step == 1,100,2)
the function g
is correct, but I do not understand what is happening to the values that are being passed to it.
This expression...
min + actualmax
...is the problem.
You probably want to make that...
+min + +actualmax
...or use parseFloat()
, parseInt()
or whatever suits your requirements to turn those strings into an actual Number
.
JavaScript's +
operator is overloaded for arithmetic addition and string concatenation. Because user input is always a string, you're doing string concatenation.
这篇关于javascript表单编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!