繁体   English   中英

如何禁用<enter> react-bootstrap 中表单提交的关键</enter>

[英]how to disable <ENTER> key for form submit in react-bootstrap

在以下代码段中,我有许多输入 forms 是文本类型。 如果用户点击,我似乎得到了与他们按下提交按钮相同的合成事件。 我想忽略作为表单提交,只允许按下提交按钮。 (我删除了一些表单组以减少示例)。

在所有情况下(按钮或 ENTER 键)

e.key is undefined
e.which is undefined
e.type is submit
e.target is the submit button 

(这是一个合成事件)

const React = require('react')
import { Form, FormControl, FormGroup, ControlLabel, Col, Button, Tooltip, OverlayTrigger } from 'react-bootstrap'

const Configuration = ({ ServerPort, UserID, PortNumber, WWWUrl, SourcePath, FMEPath }) => {
    const buttonAction = (e) => {
        e.preventDefault();
        alert(e.target.innerHTML)
    }
    return (
        <Form horizontal>
            <FormGroup controlId="serverPortBox">
                <Col componentClass={ControlLabel} sm={2}>Watson Port:</Col>
                <Col sm={10}>
                    <OverlayTrigger placement="left" overlay={(<Tooltip id="tt1">TCP port for Watson to use</Tooltip>)}>
                        <FormControl type="number" min="1024" max="65535" placeholder={ServerPort} />
                    </OverlayTrigger>
                </Col>
            </FormGroup>
            <FormGroup controlId="dbPortBox">
                <Col componentClass={ControlLabel} sm={2}>Database Port:</Col>
                <Col sm={10}>
                    <OverlayTrigger placement="left" overlay={(<Tooltip id="tt3">TCP port for PostGreSql DB</Tooltip>)}>
                        <FormControl type="number" min="1024" max="65535" placeholder={PortNumber} />
                    </OverlayTrigger>
                </Col>
            </FormGroup>
            <Button type="submit" bsStyle="primary" block onClick={(e) => buttonAction(e)}>Update Configuration</Button>
        </Form>
    )
}

export default Configuration

最简单的解决方案是使用type="button"而不是type="submit"

也许应该插入“按钮”类型的按钮而不是“提交” 然后只需处理onClick这个按钮?

我找到了解决方法如何在不使用type="button"情况下实现它

const handleKeyDown = (event, callback) => {
  if (event.key === 'Enter' && event.shiftKey === false) {
    event.preventDefault();
    callback(submitAddress);
  }
};

和形式的变化:

<form onSubmit={handleSubmit(submitAddress)} onKeyDown={e => { handleKeyDown(e, handleSubmit) }}>
<Button type="button">...</Button>

将类型从提交更改为按钮

React:如何提交表单<textarea>&lt;i&gt;with enter key and without a submit button with React Hooks?&lt;/div&gt;&lt;/i&gt;&lt;b&gt;带有回车键且没有带有 React Hooks 的提交按钮?&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> Halo 伙计们,我对反应很陌生,我想提交一个只有文本区域的表单,并按下enter键。 我遵循了一些 SO 问题,但仍然没有运气,因为它没有被提交。 我还想在提交后清除文本区域。 我怎样才能用下面的代码做到这一点?</p><p> 这是我目前拥有的代码。</p><pre> const { register, handleSubmit, control, errors } = useForm(); const CommentOnSubmit = (data) =&gt; { let formData = new FormData(); formData.append('content', data.content); formData.append('user', user?.id); axiosInstance.post(`api/blogs/` + slug + `/comment/`, formData); }; // const commentEnterSubmit = (e) =&gt; { // if(e.key === 'Enter' &amp;&amp; e.shiftKey == false) { // return( // e.preventDefault(), // CommentOnSubmit() // ) // } // } &lt;form noValidate onSubmit={handleSubmit(CommentOnSubmit)}&gt; &lt;div className="post_comment_input"&gt; &lt;textarea type="text" placeholder="Write a comment..." name="content" ref={register({required: true, maxLength: 1000})} /&gt; &lt;/div&gt; &lt;div className="comment_post_button"&gt; &lt;Button type="submit" variant="contained" color="primary"&gt;comment&lt;/Button&gt; &lt;/div&gt; &lt;/form&gt;</pre><p> 请帮忙。</p><p> 非常感谢。</p></div>

[英]React: How to submit a form with <textarea> with enter key and without a submit button with React Hooks?

暂无
暂无

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

相关问题 如何使用回车键禁用表单提交? 如何在 React Material UI 自动完成表单中禁用 ENTER 键 禁用输入以提交表单 如何使用ENTER禁用仅提交表单 如何在 React-Bootstrap 中禁用轮播控制按钮的宽度? React:如何提交表单<textarea>&lt;i&gt;with enter key and without a submit button with React Hooks?&lt;/div&gt;&lt;/i&gt;&lt;b&gt;带有回车键且没有带有 React Hooks 的提交按钮?&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> Halo 伙计们,我对反应很陌生,我想提交一个只有文本区域的表单,并按下enter键。 我遵循了一些 SO 问题,但仍然没有运气,因为它没有被提交。 我还想在提交后清除文本区域。 我怎样才能用下面的代码做到这一点?</p><p> 这是我目前拥有的代码。</p><pre> const { register, handleSubmit, control, errors } = useForm(); const CommentOnSubmit = (data) =&gt; { let formData = new FormData(); formData.append('content', data.content); formData.append('user', user?.id); axiosInstance.post(`api/blogs/` + slug + `/comment/`, formData); }; // const commentEnterSubmit = (e) =&gt; { // if(e.key === 'Enter' &amp;&amp; e.shiftKey == false) { // return( // e.preventDefault(), // CommentOnSubmit() // ) // } // } &lt;form noValidate onSubmit={handleSubmit(CommentOnSubmit)}&gt; &lt;div className="post_comment_input"&gt; &lt;textarea type="text" placeholder="Write a comment..." name="content" ref={register({required: true, maxLength: 1000})} /&gt; &lt;/div&gt; &lt;div className="comment_post_button"&gt; &lt;Button type="submit" variant="contained" color="primary"&gt;comment&lt;/Button&gt; &lt;/div&gt; &lt;/form&gt;</pre><p> 请帮忙。</p><p> 非常感谢。</p></div> 按Enter键时禁用以html格式提交表单 在文本区域上按Enter键时如何提交表单? 如何使用ENTER键提交表单 在React-Bootstrap中有条件地禁用选项卡
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM