【问题标题】:How to add a user input with an output in Javascript react/material ui如何在 Javascript react/material ui 中添加用户输入和输出
【发布时间】:2021-08-12 17:47:01
【问题描述】:

我正在做一个项目,需要它在 react js 和材料 UI 中。我想知道如何实现下面的代码以适应我需要的格式,这只是一个 js 文件。

//js file

function calculate(){
var input1Value = document.getElementById('input1').value;
var input2Value = document.getElementById('input2').value;
  
var output1 = parseInt(input1Value) * 4.5;
document.getElementById('output1').value = output1;
  
var output2 = parseInt(output1) + parseInt(input2Value);
document.getElementById('output2').value = output2;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//.HTML file

Input1 : <input type="text" id="input1" value="25000" />
Input2 : <input type="text" id="input2" value="30000" />
<button onClick="calculate();">Calculate</button>

Output1 : <input type="text" id="output1" />
Output2 : <input type="text" id="output2" />

【问题讨论】:

  • 展示你的尝试,或者你不知道 React?
  • @Odunsi 我是新手,所以我不太确定该怎么做

标签: javascript reactjs material-ui


【解决方案1】:
class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
        input1: 25000,
        input2: 30000,
        output1: '',
        output2: ''
    };

    this.handleChange1 = this.handleChange1.bind(this);
    this.handleChange2 = this.handleChange2.bind(this);
    this.calculate = this.calculate.bind(this);
  }

  handleChange1(event) {
    this.setState({input1: event.target.value});
  }

  handleChange2(event) {
    this.setState({input2: event.target.value});
  }

  calculate(event) {
    this.setState({
        output1: parseInt(this.state.input1) * 4.5,
        output2: parseInt(this.state.output1) + parseInt(this.state.input2)
    });
  }

  render() {
    return (
        Input1 : <input type="text" id="input1" value="{this.state.input1}"  onChange={this.handleChange1}/>
        Input2 : <input type="text" id="input2" value="{this.state.input2}"  onChange={this.handleChange2}/>
        <button onClick="{this.calculate}">Calculate</button>

        Output1 : <input type="text" id="{this.state.output1}" />
        Output2 : <input type="text" id="{this.state.output2}" />
    );
  }
}

【讨论】:

  • 忽略我对错误的评论,我已修复它@Nikita
  • 是否可以让代码显示在带有文本框和功能计算按钮的页面上@Nikita
  • 你是如何开始使用 react 应用程序的?
  • react 应用程序编译得很好,但结果没有显示出来@Nikita
猜你喜欢
  • 1970-01-01
  • 2021-09-27
  • 2022-11-11
  • 2019-11-26
  • 2020-02-12
  • 2022-01-21
  • 1970-01-01
  • 2017-03-28
  • 2021-02-12
相关资源
最近更新 更多