numerical value of number input by user in a text field

后端 未结 4 1247
灰色年华
灰色年华 2020-12-06 19:47

I need to add up two numbers input by the user. To do that, I create two input fields, retrieve values from them , using .val(), in two separate variables and then add them.

4条回答
  •  半阙折子戏
    2020-12-06 20:10

    Either use parseInt (http://www.w3schools.com/jsref/jsref_parseint.asp) or parseFloat (http://www.w3schools.com/jsref/jsref_parsefloat.asp) to convert to a numerical value before adding.

    PS: This is the simple answer. You might want to do some validation/stripping/trimming etc.

提交回复
热议问题