在html的js中,需要达到两数值相加怎么办?简单来说就是用parseInt(1)+parseInt(2)
直接上方法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 数字1<input type="text" id="add1"><br> 数字2<input type="text" id="add2"><br> <button onclick="getAdd()">x相加</button> <br> <textarea name="" id="input" cols="30" rows="10"> </textarea> <script> function getAdd(){ var num1 = document.getElementById("add1").value; var num2 = document.getElementById("add2").value; var sum = parseInt(num1)+parseInt(num2) document.getElementById("input").value = sum; alert("数字:"+num1+" \n数字:"+num2+" \n和:"+sum); console.log("数字1:"+num1) console.log("数字2:"+num2) console.log("和"+sum) } </script> </body> </html>