最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Basic Javascript math text field - Stack Overflow

programmeradmin4浏览0评论

hello im new and learning javascript.

I'm trying to make a program of addition through text field.

Check the html code on js fiddle /

What I need to know is how can I accept user input in text field and diplay output in P tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>untitled</title>
</head>
<body>
<input id="value1" type="text" />
<span> + </span>
<input id="value2" type="text" />
<input type="submit" onclick="output();">
<p id="result"> </p>

<script type="text/javascript" language="javascript" charset="utf-8">
var value1 = document.getElementById('value1').innerHTML;
var value2 = document.getElementById('value2').innerHTML;

function output(){
    document.getElementById('result').innerHTML = value1 + value2;
}

</script>
</body>
</html>

hello im new and learning javascript.

I'm trying to make a program of addition through text field.

Check the html code on js fiddle http://jsfiddle/fCXMt/

What I need to know is how can I accept user input in text field and diplay output in P tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>untitled</title>
</head>
<body>
<input id="value1" type="text" />
<span> + </span>
<input id="value2" type="text" />
<input type="submit" onclick="output();">
<p id="result"> </p>

<script type="text/javascript" language="javascript" charset="utf-8">
var value1 = document.getElementById('value1').innerHTML;
var value2 = document.getElementById('value2').innerHTML;

function output(){
    document.getElementById('result').innerHTML = value1 + value2;
}

</script>
</body>
</html>
Share Improve this question edited Jun 18, 2011 at 11:55 jonsca 10.4k26 gold badges55 silver badges63 bronze badges asked Jun 18, 2011 at 11:48 kevvvinkevvvin 431 gold badge1 silver badge3 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

You have to grab the values in input fields after the button click, and use the value property (not innerHTML) to do it. Also, make sure you're adding numbers and not appending strings together. Try this:

function output(){
    var value1 = document.getElementById('value1').value;
    var value2 = document.getElementById('value2').value;

    document.getElementById('result').innerHTML = parseInt(value1) + parseInt(value2);
}

The property for getting the value of a textbox is value, not innerHTML, so change those, and you will also need to use eval or parseInt on the textbox values, otherwise it will concatenate them as strings.

Also, you need to move your variable declarations inside the function, so that when the function is called, the current values from the textboxes are retreived.

See update fiddle here.

You need to access the "value" property of the input fields and parse them as integers:

var value1 = parseInt(document.getElementById('value1').value);
var value2 = parseInt(document.getElementById('value2').value);

You only read the values once, you should read them in the output function; You need to parse them to integers as well since 1+4 will bee 14 if you use strings. Could be better but this should work.

<script type="text/javascript" language="javascript" charset="utf-8">

function output(){
    var value1 = parseInt(document.getElementById('value1').value);
    var value2 = parseInt(document.getElementById('value2').value);
    document.getElementById('result').innerHTML = value1 + value2;
}

</script>

For pleteness: your scripting could be better. Based on your form I cooked up another jsfiddle example.

While getting value, just multiple it by 1, it will parse the value into number

const value1 = document.getElementById('value1').innerHTML.value * 1;
const value2 = document.getElementById('value2').innerHTML.value * 1;
document.getElementById('result').innerHTML = value1 + value2;
发布评论

评论列表(0)

  1. 暂无评论