如何获得输出? 代码中存在一些问题,我无法得到答案。 而我无法确定这是我的错误
<!Doctypehtml>
<html>
<title>new html</title>
<head>
</head>
<body>
<script >
var number = document.getElementById("num1");
var number2 = document.getElementById("num2");
var out = document.getElementById("output");
number.addEventListener("input", sol);
number2.addEventListener("input", sol);
function sol(){
var one = parseint(number.value);
var two = parseint(number2.value);
out.innerHTML = one+two;
}
</script>
<input id="num1">+<input id="num2">
<p id="output"></p>
</body>
</html>
答案 0 :(得分:2)
将parseint更改为parseInt:
$(document).ready(function () {
var number = document.getElementById("num1");
var number2 = document.getElementById("num2");
var out = document.getElementById("output");
number.addEventListener("input", sol);
number2.addEventListener("input", sol);
function sol() {
var one = parseInt(number.value);
var two = parseInt(number2.value);
out.innerHTML = one + two;
}
});
<强> Demo 强>
答案 1 :(得分:1)
该功能为parseInt
(首都为I
)而不是parseint
正如@Dai
var one = parseInt(number.value);
var two = parseInt(number2.value);
以下是Fiddle
还有理由在 innerHTML 上使用 textcontext
使用isNaN()
函数确定值是否为非法数字(非数字)。
if (!isNaN(three))
{
//if its a valid number try to display the value to the output
}
答案 2 :(得分:0)
我想这正是你要找的东西:
<强> Working : Demo 强>
var number = document.getElementById("num1");
var number2 = document.getElementById("num2");
var out = document.getElementById("output");
function sol(){
var one = parseInt(number.value);
var two = parseInt(number2.value);
if(number2.value != '')
{out.innerHTML = one+two;}
else
{out.innerHTML = '';}
}
&#13;
<input id="num1">+<input id="num2" onkeyup="sol()">
<p id="output"></p>
&#13;
答案 3 :(得分:0)
嘿首先尝试将java脚本文件嵌入到代码中,除了编写内联JS代码,因为如果代码很大,那么通过确定HTML中的ID值,您可能会陷入向ID分配事件的困境。 其次,是的,您在以下代码中有错误
function sol(){
var one = parseint(number.value);
var two = parseint(number2.value);
out.innerHTML = one+two;
}
/ *需要更改以下行 / / parseInt * /
var one = parseInt(number.value);
var two = parseInt(number2.value);
谢谢!!