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

标签 reactjs

我创建了一个小表单,用户可以在其中键入一个数字并按 Enter 键提交。

目前,我创建了一个隐藏的 Button,它具有 onClick={handleSubmit}type="submit" 属性。 这给用户一种按钮不存在的错觉。 一切正常。

然而 我认为创建 Button 只是为了给它 onClick 并隐藏它,这是多余的。

我想知道是否有其他方法可以在不创建按钮的情况下提交表单。

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>
    )

}

尝试添加onSubmit Prop <Form/>

关于reactjs - 无需单击按钮即可提交 React 表单 - React Bootstrap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58800092/

相关文章:

css - 如何在 React 中为伪类动态添加 css 样式

javascript - 将(外部)组件动态加载到 React.js 中

javascript - 以 Redux 形式动态加载 initialValues

reactjs - 鼓机播放相同的声音

javascript - activeClassName 不适用于可变路径的 react 路由器

javascript - 当不在主路径中时,显示后退按钮以像浏览器后退按钮一样返回

javascript - 未捕获的类型错误 : Cannot read property 'props' of undefined in reactJS when uploading image

reactjs - Node Sass 版本 7.0.0 与 ^4.0.0 不兼容 || ^5.0.0 || ^6.0.0

node.js - 是否可以使用 JSX 进行 React 工作,无需 Node 服务器并使用 create-react-app

reactjs - 使用具有日期时间本地类型的material-ui TextField 时是否会阻止输入清除?