如何在react-bootstrap

时间:2017-05-03 18:50:41

标签: javascript reactjs html-form react-bootstrap

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

在所有情况下(按钮或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

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

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

答案 2 :(得分:0)

我找到了解决方案,该方法如何不使用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) }}>