简体   繁体   English

Event.target在React类型按钮中返回未定义

[英]Event.target returns undefined in React type button

I have a React form that has one submit button of type='submit' and another one which is a delete button of type='button' . 我有一个React表单,其中有一个type='submit' 提交按钮,另一个有一个type='button'删除按钮。 My problem is I couldn't get the event.target that I want in the onClick function of the delete button. 我的问题是我无法在删除按钮的onClick函数中获得想要的event.target

handleSubmit = (event) => {
  event.target.testing.value    // This will work
}

handleDelete = (event) => {     
  event.target.testing.value    // This doesn't work, returns undefined
}

render(){
  return(
    <form onSubmit={this.handleSubmit}>
       <input id='testing' value='anything' />

       <button type='submit'/>
       <button type='button' onClick={this.handleDelete} />
    </form>
  )
}

I understand that in the handleDelete function, the event param is the event of the button so if I do event.target.value it will give me the value of the button if the button has the value property. 我知道在handleDelete函数中,事件参数是按钮的事件,因此如果我执行event.target.value ,则如果按钮具有value属性,它将为我提供按钮的值。

Is there a way so that I can access the value of the input in the handleDelete function? 有没有办法可以访问handleDelete函数中input的值?

Before moving forward, you should read this . 在继续之前,您应该阅读此内容

Then, fix your code (assuming you're using class fields , if not, follow the example in the link above)... 然后,修复您的代码(假设您正在使用类字段 ,如果没有,请按照上面链接中的示例操作)...

 class Example extends React.Component { state = { value: "" }; handleClear = (event) => { // assuming you want to clear the input // you won't need the event if you utilize state this.setState({ value: "" }); } handleChange = (event) => { const { value } = event.target; // same as "value = event.target.value" this.setState({ value }); } handleSubmit = (event) => { event.preventDefault(); // prevents the page from freshing when "submit" is clicked alert(this.state.value); } render() { return ( <form onSubmit={this.handleSubmit}> <label htmlFor="testInput">Input: </label> <input id="testInput" type="text" value={this.state.value} onChange={this.handleChange} /> <button type="button" onClick={this.handleClear}>Clear</button> <button type="submit">Submit</button> </form> ); } } ReactDOM.render(<Example />, document.body); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> 

Because they are brother elements, button cannot get his siblings by event.target. 因为它们是兄弟元素,所以button无法通过event.target获得其兄弟姐妹。 If you want to change the value. 如果要更改值。 I recommand bind state to your input value, and then if you want to delete, setState input value to ''. 我建议将绑定状态设置为您的输入值,然后如果要删除,请将setState输入值设置为''。

This is because you need to bind the reference to this to the event handler 这是因为您需要将this的引用绑定到事件处理程序

You need to either do the binding in the constructor like this: 您需要像这样在构造函数中进行绑定:

constructor(props) {
   super(props);

   this.handleSubmit = this.handleSubmit.bind(this);
}

Or you could also do the binding like this: 或者,您也可以像这样进行绑定:

<button type='button' onClick={this.handleDelete.bind(this} />

Read this, it´s very clear and well written as this is a common question: React docs 阅读此书,它非常清楚且写得很好,因为这是一个常见问题: React docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM