简体   繁体   English

无需单击按钮即可提交反应表单 - React Bootstrap

[英]Submit a react form without clicking a button - React Bootstrap

I created a small form where the user types a number and submits it by pressing the Enter key.我创建了一个小表单,用户在其中键入一个数字并按 Enter 键提交。

Currently, I created a hidden Button which has the onClick={handleSubmit} and type="submit" properties.目前,我创建了一个具有onClick={handleSubmit}type="submit"属性的隐藏Button This gives the user the illusion that the button does not exists.这给用户一种按钮不存在的错觉。 Everything works.一切正常。

HOWEVER I think that the creation of the Button just that I can give it the onClick and the hide it, it is redundant.但是我认为Button的创建只是我可以给它onClick并隐藏它,这是多余的。

I am wondering if there are other ways to submit a form without creating a button.我想知道是否有其他方法可以在不创建按钮的情况下提交表单。

function SingleInventoryChanger({single_inventory, single_date}) {
    const [newInventory, setNewinventory] = React.useState([{single_inventory}]);
    console.log(newInventory)

    function handleChange(event) {
            setNewinventory(event.target.value)
    }


    function handleSubmit(event){
        event.preventDefault();
        console.log(newInventory)
        console.log(single_date)
    }


    return(
        <div>
            <Form>
                <Form.Row>
                    <Col>
                        <Form.Control placeholder={single_inventory} onChange={handleChange} />
                    </Col>
                </Form.Row>
                <Button variant="secondary" type="submit" onClick={handleSubmit} > # I am referring to this button
                </Button> # I am referring to this button
            </Form>
        </div>
    )

}
function SingleInventoryChanger({single_inventory, single_date}) {
    const [newInventory, setNewinventory] = React.useState([{single_inventory}]);
    console.log(newInventory)

    function handleChange(event) {
            setNewinventory(event.target.value)
    }


    function handleSubmit(event){
        event.preventDefault();
        console.log(newInventory)
        console.log(single_date)
    }


    return(
        <div>
            <Form onSubmit={handleSubmit}>
                <Form.Row>
                    <Col>
                        <Form.Control placeholder={single_inventory} onChange={handleChange} />
                    </Col>
                </Form.Row>
            </Form>
        </div>
    )

}

Try to add onSubmit props on <Form/>尝试在<Form/>上添加onSubmit道具

You can try this:你可以试试这个:

function SingleInventoryChanger({single_inventory, single_date}) {
const [newInventory, setNewinventory] = React.useState([{single_inventory}]);
const formRef = useRef(null)
console.log(newInventory)

function handleChange(event) {
        setNewinventory(event.target.value)
}


function handleSubmit(event){
    event.preventDefault();
    console.log(newInventory)
    console.log(single_date)
}
const handleKeyDown = (ev)=>{
   if(ev.keyCode ===13){ // enter button
    formRef.current.submit()
   }
}

return(
    <div onKeyDown={handleKeyDown}>
        <Form ref={formRef} onSubmit={handleSubmit}>
            <Form.Row>
                <Col>
                    <Form.Control placeholder={single_inventory} onChange={handleChange} />
                </Col>
            </Form.Row>
        </Form>
    </div>
)

}

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.

相关问题 单击提交按钮后使用反应清除表单中的受控组件 - clear controlled components in a form using react after clicking submit button 单击提交按钮后如何使用反应重置表单? - How to reset a form after clicking submit button by using react? 检查是否填写了 React Bootstrap Form 以有条件地禁用提交按钮 - Check if React Bootstrap Form is filled in or not to conditionally disable submit button react-bootstrap 表单按钮在第一次点击时不提交 - react-bootstrap form button doesn't submit on first click React Bootstrap,通过 React Hooks 提交表单 - React Bootstrap, submit form via React Hooks 单击react中的Submit按钮迁移到新组件 - Migrate to new component on clicking submit button in react 如何在React中点击图片提交表单? - How to submit a form by clicking on an image in react? 反应表单中的提交按钮未提交 - Submit button in react form not submitting 在 React 中禁用表单提交上的按钮 - Disable button on Form Submit in React 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM