基本Javascript数学文字栏位 [英] Basic Javascript math text field

查看:103
本文介绍了基本Javascript数学文字栏位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是新手并且正在学习JavaScript。

hello im new and learning javascript.

我正在尝试通过文本字段制作加法程序。

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

检查js小提琴上的html代码 http://jsfiddle.net/fCXMt/

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

我需要知道的是如何接受文本字段中的用户输入以及 P 标签中的显示输出。

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>


推荐答案

用于获取文本框值的属性为 value ,而不是 innerHTML ,因此请更改这些值,您还需要使用 eval parseInt 放在文本框值上,否则它将它们连接为字符串。

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.

请参见update fiddle 此处

See update fiddle here.

这篇关于基本Javascript数学文字栏位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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