将变量反馈到html输入中

时间:2017-08-16 07:46:23

标签: javascript html reactjs

我的.js中有一个React变量this.status.result,它保存了我的值(它是令人耳目一新的值和更改值。

我想把它放在我的HTML代码中,但我不知道该怎么做。我想要这样的东西:

    <div id="demo-container"></div>
    <script src="bundle_test.js" type="text/javascript" charset="utf-8"></script>

    <form method="post">
    <input type="text" value=this.status.result>
    <input type="submit">
    </form>

有没有简单的解决方案?我知道我可以用React创建一个表单,但是因为我的项目不起作用。

编辑:.js文件超过25k行。

3 个答案:

答案 0 :(得分:1)

在react js组件中使用变量时应该放置{}:

  <input type="text" value={this.status.result}>

答案 1 :(得分:1)

为了能够在你的html中使用props的值,你需要在{this.props.propName}这样的括号中包含它。对于您的代码,只需进行此更改:

<div id="demo-container"></div>
<script src="bundle_test.js" type="text/javascript" charset="utf-8"></script>

<form method="post">
<input type="text" value={this.status.result}>
<input type="submit">
</form>

答案 2 :(得分:0)

在组件的渲染功能中:

render() {

    const result = this.status.result;

    return (
        <form method="post">
            <input type="text" value={result}>
            <input type="submit">
        </form>
    )
}