javascript表单编号 [英] javascript form numbers

查看:119
本文介绍了javascript表单编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一天使用Javascript,我感到非常困惑。我将从表单返回的数字传递给函数,但结果与它应该是不一致的。我的测试留下了很多希望,但希望以下是有道理的。

函数 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.

jsFiddle.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆