Jas*_*rne 81 reactjs reactjs-flux
这是我的表单和onClick方法.我想在按下键盘的Enter按钮时执行此方法.怎么样 ?
注意:没有赞赏jquery.
 comment: function (e) {
        e.preventDefault();
        this.props.comment({comment: this.refs.text.getDOMNode().value, userPostId:this.refs.userPostId.getDOMNode().value})
    },
 <form className="commentForm">
     <textarea rows="2" cols="110" placeholder="****Comment Here****" ref="text"  /><br />
    <input type="text" placeholder="userPostId" ref="userPostId" /> <br />
     <button type="button" className="btn btn-success" onClick={this.comment}>Comment</button>
    </form>
Run Code Online (Sandbox Code Playgroud)
    Dom*_*nic 147
更改<button type="button"到<button type="submit".删除onClick.相反<form className="commentForm" onSubmit={this.onFormSubmit}>.这应该抓住点击按钮并按回车键.
编辑:正如詹姆斯在评论中所说,你也可以<button type="button"在回调中调用以阻止页面尝试加载<button type="submit"网址.
如果你想onClick在回调中使用你可以使用箭头函数:<form className="commentForm" onSubmit={this.onFormSubmit}>或<button type="button"在构造函数甚至e7的箭头方法<button type="submit".
onFormSubmit = e => {
  e.preventDefault();
  const { name, email } = this.state;
  // send to server with e.g. `window.fetch`
}
...
<form onSubmit={this.onFormSubmit}>
  ...
  <button type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
        use*_*613 38
距离上次回答这个问题已经有好几年了。React 在 2017 年引入了“Hooks”,而“keyCode”已被弃用。
现在我们可以这样写:
  useEffect(() => {
    const listener = event => {
      if (event.code === "Enter" || event.code === "NumpadEnter") {
        console.log("Enter key was pressed. Run your function.");
        event.preventDefault();
        // callMyFunction();
      }
    };
    document.addEventListener("keydown", listener);
    return () => {
      document.removeEventListener("keydown", listener);
    };
  }, []);
Run Code Online (Sandbox Code Playgroud)
keydown当组件第一次加载时,这会在事件上注册一个监听器。当组件被销毁时,它会删除事件侦听器。
Cyr*_*Zei 13
如果您想听“Enter”键,这就是您的方法。您可以使用一个 onKeydown 道具,您可以在react doc 中阅读它
这是一个 代码沙盒
const App = () => {
    const something=(event)=> {
        if (event.keyCode === 13) {
            console.log('enter')
        }
    }
return (
    <div className="App">
        <h1>Hello CodeSandbox</h1>
        <h2>Start editing to see some magic happen!</h2>
        <input  type='text' onKeyDown={(e) => something(e) }/>
    </div>
);
}
Run Code Online (Sandbox Code Playgroud)
        am0*_*0wa 10
使用keydown事件来做到这一点:
   input: HTMLDivElement | null = null;
   onKeyDown = (event: React.KeyboardEvent<HTMLDivElement>): void => {
      // 'keypress' event misbehaves on mobile so we track 'Enter' key via 'keydown' event
      if (event.key === 'Enter') {
        event.preventDefault();
        event.stopPropagation();
        this.onSubmit();
      }
    }
    onSubmit = (): void => {
      if (input.textContent) {
         this.props.onSubmit(input.textContent);
         input.focus();
         input.textContent = '';
      }
    }
    render() {
      return (
         <form className="commentForm">
           <input
             className="comment-input"
             aria-multiline="true"
             role="textbox"
             contentEditable={true}
             onKeyDown={this.onKeyDown}
             ref={node => this.input = node} 
           />
           <button type="button" className="btn btn-success" onClick={this.onSubmit}>Comment</button>
         </form>
      );
    }
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           74752 次  |  
        
|   最近记录:  |