【发布时间】:2018-10-24 12:13:25
【问题描述】:
您好,我是 React 和 js 的新手。
我正在尝试编写两个文本框,以便用户插入两个数字,然后单击“添加”按钮,他会得到结果。
我的建议是:
import React, { Component } from 'react';
export class Home extends Component {
displayName = Home.name
render() {
return (
<div>
<h1>Hello world! This is a new Web App</h1>
<input type="text" name="" id="txt1" />
+
<input type="text" name="" id="txt1" />
= <br/>
<button onClick="add()">Add</button>
</div>
);
}
}
我认为还有一个函数 add() 将执行以下操作:
function add()
{
var a = getElementById("txt1").value;
var b = getElementById("txt2").value;
alert(pardeInt(a)+parseInt(b));
}
如果有人可以编辑我建议的代码以使其正常工作,那就太好了。
提前致谢。
【问题讨论】:
-
这看起来你还没有阅读关于如何在反应中使用事件处理程序和表单控件的第一件事。试试reactjs.org/docs/handling-events.html 和reactjs.org/docs/forms.html
-
您只是在使用 react 来呈现 HTML 并使用 javascript。这与实际反应无关。请先阅读有关反应组件的更多信息。也许可以遵循一些教程?
标签: javascript reactjs