Plus Arithmetic Operation

后端 未结 7 1681
庸人自扰
庸人自扰 2020-12-07 06:43

I\'ve tried use Plus Arithmetic Operation to calculate 2 input text type number values, but my result was the values are \"together\", like that:

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 07:04

    You need to parse them to an Integer type:

    var x = parseInt(plus) + parseInt(plus1);
    

    Then you can use isNaN() to determine whether it was a valid operation or not: (note that I'm checking the value of x not of the resulting output)

    if(isNaN(x)){
     result.innerHTML = "?";
    }
    else{
     result.innerHTML = x;
    }
    

    Living demo: http://jsfiddle.net/4KB5Y/3/

提交回复
热议问题