即使在解析输入值后,我也得到了NaN作为答案

问题描述 投票:1回答:4

// html代码计算

两个数字的加法

var x = parseFloat(document.getElementById("n1").value); //input value1
var y = parseFloat(document.getElementById("n2").value); //input value2

var btn = document.getElementById("ans");
btn.addEventListener("click",function(){
      var result = x + y ;    //expression
      console.log(result);   // output as 'Nan'
      console.log(typeof(result)); // output as 'number'

});
javascript html dom
4个回答
0
投票

这里的问题是,您已将默认值设置为xy,开头是空字符串,例如:

var x = parseFloat(document.getElementById("n1").value); //input value1
var y = parseFloat(document.getElementById("n2").value); //input value2

因此,无论您在输入中输入什么值,都将始终获得Nan。这是现在正在发生的事情:

var x = parseFloat(""); //input value1
var y = parseFloat(""); //input value2

var btn = document.getElementById("ans");
btn.addEventListener("click", function() {
  var result = x + y; //expression
  console.log(result); // output as 'Nan'
  console.log(typeof(result)); // output as 'number' 
});
<button id="ans">ADD</button>

解决此问题的唯一方法是将变量放在click内,以便在单击按钮时只有您才能获得每个输入的最新值,例如:

var btn = document.getElementById("ans");
btn.addEventListener("click", function() {
  var x = parseFloat(document.getElementById("n1").value); //input value1
  var y = parseFloat(document.getElementById("n2").value); //input value2
  var result = x + y;
  console.log(result);
  console.log(typeof(result));
});
<input type="text" id="n1">
<input type="text" id="n2">
<button id="ans">ADD</button>

此外,如果您仅在此处使用数字,则可以使用<input type="number" />代替<input type="text" />,例如:

var btn = document.getElementById("ans");
btn.addEventListener("click", function() {
  var x = parseFloat(document.getElementById("n1").value); //input value1
  var y = parseFloat(document.getElementById("n2").value); //input value2
  var result = x + y;
  console.log(result);
  console.log(typeof(result));
});
<input type="number" id="n1">
<input type="number" id="n2">
<button id="ans">ADD</button>

0
投票

请确保在页面加载后执行您的JavaScript。例如:

document.onload = function () {}

window.onload = function () {}

<body onload="..">

或带有

<script ... defer>

否则,.value将返回未定义,结果将为NaN。


0
投票

如果在发生任何事件之前访问输入字段的值,我们将无法定义,因此我们可以在生成click事件之后访问该字段的值。

<input type="text" id="n1" name="name">
    <input type="text" id="n2" name="emai">
    <input type="submit" id="ans" value="Submit">

    <script>


        var btn = document.getElementById("ans");
        btn.addEventListener("click",function(){
            var x = parseFloat(document.getElementById("n1").value); 
            var y = parseFloat(document.getElementById("n2").value); 
              var result = x + y ;    //expression
              console.log(result);   // output as 'Nan'
              console.log(typeof(result)); // output as 'number'

        });
    </script>

0
投票

let btn = document.getElementById("ans")
btn.addEventListener("click", function() {
	let x = getFloat(getElVal("n1"))
	let y =  getFloat(getElVal("n2"))
	let result = x + y

	if (!isNaN(x) && !isNaN(y)) {
		console.log('valid op')
	}

	console.log(result);
	console.log(typeof(result))
	
	function getFloat(val) {
		return parseFloat(val)
	}

	function getElVal(id) {
		ele = document.getElementById(id)
		return ele? ele.value:undefined
	}
})
<input type="text" id="n1">
<input type="text" id="n2">
<button id="ans">ADD</button>
© www.soinside.com 2019 - 2024. All rights reserved.