如何将用户输入设置为输入值? [英] how do I set input from user to the value of an input?

查看:74
本文介绍了如何将用户输入设置为输入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的程序来计算三角形的面积,但我发现很难从用户那里获得输入。我给输入一个id并将值设置为输入的innerhtml,但它给了我一个默认值1.




I'm trying to write a simple program to calculate the area of a triangle but I'm finding it hard to get input from user. I gave the input an id and set the value to the innerhtml of the input but it's giving me a default value of 1.

<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Area Of A Triangle</title>

<style type="text/css">


p{
padding:0;
margin:0;
}

</style>

</head>

<body>

<h2>Calculate The Area Of A Triangle</h2>

<p>Enter The Values</p>

<p><input id="firstside"  type="text" /></p>
<p><input id="secondside" type="text" /></p>
<p><input id="thirdside" type="text" /></p>

<button id="solve">Submit</button>

<p>Answer Is : <span id="answer"></span></p>


<script type="text/javascript" >

var a; var b; var c; var s; var area;

a= document.getElementById("firstside").value==document.getElementById("firstside").innerHTML;
b= document.getElementById("secondside").value==document.getElementById("secondside").innerHTML;
c= document.getElementById("thirdside").value==document.getElementById("thirdside").innerHTML;

document.getElementById("solve").onclick=function() {

s = (a+b+c);

document.getElementById("answer").innerHTML=s;

}

</script>

</body>
</html>


推荐答案

当您阅读 innerHTML时属性,将元素的所有子节点序列化为HTML,然后将其作为字符串。设置 innerHTML 反过来做同样的事情。

When you read the innerHTML property, you serialize all the child nodes of the element to HTML and then get that as a string. Setting innerHTML does the same thing in reverse.

输入元素没有(也不能)任何子节点(< input> foo< / input> )都是无效的HTML)。

An input element doesn't (and can't) have any child nodes (<input>foo</input>) is invalid HTML).

其当前值通过其属性反映出来。

Its current value is reflected through its value property.

这篇关于如何将用户输入设置为输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆