gpt4 book ai didi

javascript - 使用 Enter Key React 触发提交按钮

转载 作者:行者123 更新时间:2023-11-29 11:00:33 26 4
gpt4 key购买 nike

我想按 Enter 键并发送消息。我试试这个:

但是有错误。

<input type="text" className="form-control input-sm chat_input" placeholder="Write your message here..."
onChange={this.handleTextChange} value={this.state.newMessage}
/>
<span className="input-group-btn">
<input type="submit" value="Send" className="btn btn-primary btn-sm" onSubmit={this.handleSendMessage}
/></span>

我想管理我的功能:

handleSendMessage = e => {}

我已经尝试过 OnKeyPressed 但我无法在那里调用我的函数。谢谢。

我更喜欢没有 jquery 的解决方案

最佳答案

您将在输入文本输入上添加一个 keyPress 事件,然后使用 e.which ===13

检测回车键按下
onKeyPress = (e) => {
if(e.which === 13) {
this.handleSendMessage();
}
}
render() {
return (
<div style={styles}>
<input type="text" className="form-control input-sm chat_input" placeholder="Write your message here..."
onChange={this.handleTextChange} onKeyPress={this.onKeyPress} value={this.state.newMessage}
/>
<span className="input-group-btn">
<input type="submit" value="Send" className="btn btn-primary btn-sm" onSubmit={this.handleSendMessage}
/></span>
</div>
);

}

关于javascript - 使用 Enter Key React 触发提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47873513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com