[英]ReactJS event.target.value returns as undefined
I've made a form in ReactJS with one text input and when it submits I want to get its value and put it into a variable.我在 ReactJS 中制作了一个带有一个文本输入的表单,当它提交时,我想获取它的值并将其放入变量中。 But when I console.log() it returns as
undefined
.但是当我 console.log() 它返回为
undefined
。 How do I fix this?我该如何解决? Here is my code.
这是我的代码。
class App extends Component {
state = {
todoTitle: ""
};
render() {
return (
<div>
<center>
<form
onSubmit={(event) => {
event.preventDefault();
this.setState(todoTitle: event.target.value,);
console.log(this.state.todoTitle); // Returns "undefined"
}}
>
<input
type="text"
autocomplete="off"
class="form-control"
name="todoInput"
placeholder="Enter todo"
style={{ width: "400px", height: "50px" }}
/>
<input
type="submit"
value="Submit"
id="submitButton"
></input>
</form>
</center>
}
}
}
You can modify your app a bit to get the value on onChange of input textfield, and then store it in the array in case of below example:您可以稍微修改您的应用程序以获取输入文本字段的 onChange 值,然后将其存储在数组中以防以下示例:
export default class App extends React.Component {
state = {
todoTitle: "",
todoList: []
};
render() {
return (
<div>
<center>
<form
onSubmit={event => {
event.preventDefault();
this.setState(
{
todoList: [...this.state.todoList, this.state.todoTitle]
},
() => {
console.log(this.state.todoList);
}
);
}}
>
<input
type="text"
autocomplete="off"
class="form-control"
name="todoInput"
placeholder="Enter todo"
onChange={event => {
this.setState({ todoTitle: event.target.value });
console.log(event.target.value);
}}
style={{ width: "400px", height: "50px" }}
/>
<input type="submit" value="Submit" id="submitButton" />
</form>
</center>
</div>
);
}
}
Full app here: Stackblitz完整的应用程序: Stackblitz
There are a few other errors with your code, but I will just answer your question.您的代码还有其他一些错误,但我只会回答您的问题。
setState
triggers a re-render, so your state isn't available to log until the next time it runs. setState
会触发重新渲染,因此您的 state 在下次运行之前无法记录。 You can just log what you put in setState.您可以只记录您放入 setState 的内容。
console.log(event.target.value);
This question has more info.这个问题有更多信息。 setState doesn't update the state immediately
setState 不会立即更新 state
Also, you can do a callback.此外,您可以进行回调。
this.setState({ todoTitle: event.target.value }, () =>
console.log(this.state.todoTitle)
);
Try this:尝试这个:
class App extends React.Component {
constructor(props) {
super(props);
this.state = { todoTitle: "" };
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
event.preventDefault();
this.setState({ todoTitle: event.target.value });
}
handleSubmit(event) {
event.preventDefault();
console.log(this.state.todoTitle);
}
render() {
return (
<div>
<center>
<form onSubmit={this.handleSubmit}>
<input
type="text"
autocomplete="off"
class="form-control"
name="todoInput"
placeholder="Enter todo"
style={{ width: "400px", height: "50px" }}
onChange={this.handleChange}
/>
<input type="submit" value="Submit" id="submitButton" />
</form>
</center>
</div>
);
}
}
This will change the state on input changes and then logs on submit the state.这将在输入更改时更改 state,然后登录提交 state。 An alternative would be to just get the input element and its value via getElementById or something similar in React.
另一种方法是通过 getElementById 或 React 中的类似方法获取输入元素及其值。
Your code was also not very well formatted and a lot of closing tags missed.您的代码格式也不是很好,并且错过了很多结束标签。
Read more here: Get form data in ReactJS在此处阅读更多信息: 在 ReactJS 中获取表单数据
You need to either make a controlled input
or useRef
for un-controlled input for the React to keep track of your todoTitle
state.您需要为 React 进行
controlled input
或使用非受控输入,以跟踪您的useRef
todoTitle
。
To make a controlled input
, you will need to use onChange event
and a value={this.state.todoTitle}
property.要进行
controlled input
,您需要使用onChange event
和value={this.state.todoTitle}
属性。
Also on your form, it is best to add an onSubmit
event.同样在您的表单上,最好添加一个
onSubmit
事件。 There is however an option to set the submit on the form submit button also.但是,还有一个选项可以在表单提交按钮上设置提交。 In that case we need to use
onClick={this.handleSubmit}
as follows <input type="submit" value="Submit" id="submitButton" onClick={this.handleSubmit} />
.在这种情况下,我们需要使用
onClick={this.handleSubmit}
如下<input type="submit" value="Submit" id="submitButton" onClick={this.handleSubmit} />
。
The below code will work for you:以下代码将为您工作:
class Form extends React.Component {
state = {
todoTitle: "",
};
handleSubmit = (e) => {
e.preventDefault();
console.log(this.state.todoTitle);
};
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<input
type="text"
autocomplete="off"
class="form-control"
name="todoInput"
placeholder="Enter todo"
style={{ width: "400px", height: "50px" }}
value={this.state.todoTitle}
onChange={(e) => this.setState({ todoTitle: e.target.value })}
/>
<input type="submit" value="Submit" id="submitButton" />
</form>
</div>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.