我使用material-UI文本字段,并希望从用户的输入中获取值。现在,我只是尝试将值记录到控制台,这样我就可以看到它正在获取值,但是,它记录为空。如何从文本字段获取输入?
输入卡是一个带有文本字段以及一些其他设计内容的组件。
import "./StartPage.scss";
import React, { Component } from "react";
import InputCard from '../components/InputCard';
import {BrowserRouter as Router, Switch, Route, Link} from 'react-router-dom';
import ResultsPage from '../pages/ResultsPage';
import InputToFormula from "../components/InputToFormula";
import PropTypes from 'prop-types';
import { evaluate } from "mathjs";
import * as math from "mathjs";
import { tsConstructorType } from "@babel/types";
class StartPage extends Component {
constructor(props) {
super(props);
this.state = {
setBots: "",
setEmployees: "",
setSalary: "",
setTime: ""
};
};
handleBots = event => {
this.setState({ setBots: event.target.value});
};
handleEmployees = event => {
this.setState({ setEmployees: event.target.value});
};
handleSalary = event => {
this.setState({ setSalary: event.target.value});
};
handleTime = event => {
this.setState({ setTime: event.target.value});
};
logValue = () => {
console.log(this.state.setBots);
};
render(){
return (
<div className="container">
<div className="header-container">
<h2> </h2>
<h1>Return on Investment Calculator</h1>
<h2> </h2>
</div>
<form>
<div class="inputs input-group">
<InputCard onEvent={this.handleBots} id="Bots" icon="robot" label="Number of Processes" />
<InputCard onEvent={this.handleEmployees} id="Employees" icon="users" label="Number of FTE's" />
<InputCard onEvent={this.handleSalary} id="Salary" icon="dollar-sign" label="Average Salary" />
<InputCard onEvent={this.handleTime} id="Time" icon="clock" label="Average Time (%)" />
</div>
<Router>
<div>
<h1> </h1>
<Link to='/ResultsPage'><button onClick={this.logValue} type="button"
class="btn btn-outline-success submit-button btn-lg">CALCULATE</button></Link>
</div>
<Switch>
<Route path="/ResultsPage" exact>
<ResultsPage />
</Route>
</Switch>
</Router>
</form>
</div>
);
}
}
export default StartPage;发布于 2021-07-20 00:50:49
使用onChange代替onEvent,here你可以阅读更多关于它的内容
https://stackoverflow.com/questions/68444250
复制相似问题