【问题标题】:React form onSubmit is not working and input not allow to use white spaces反应表单 onSubmit 不起作用,输入不允许使用空格
【发布时间】:2018-09-03 23:36:48
【问题描述】:

GitHub 问题:https://github.com/facebook/create-react-app/issues/4290#issuecomment-380521431

我找到了力量的东西:

    import React, { Component } from "react";
    import FieldGroup from "./FieldGroup";
    import { connect } from "react-redux";
    import { Form, Button } from "react-bootstrap";
    class UserUpdateForm extends Component {
      constructor(props) {
        super(props);
        this.handleUserUpdate = this.handleUserUpdate.bind(this);
        this.handleChangeName = this.handleChangeName.bind(this);
        this.handleChangeNickname = this.handleChangeNickname.bind(this);
        this.state = {
          name: this.props.current_user.name,
          nickname: this.props.current_user.nickname
        };
      }

      handleChangeName(event) {
        this.setState({
          name: event.target.value
        });
      }
      handleChangeNickname(event) {
        this.setState({
          nickname: event.target.value
        });
      }

      handleUserUpdate(event) {
        event.preventDefault();
        const url = event.target.action;
        console.log(url);
        return false;
      }
      render() {
        return (
          <Form
            onSubmit={this.handleUserUpdate}
            action={this.props.action}
            method="patch"
            id={this.props.id}
          >
            <FieldGroup
              role="form"
              id={`${this.props.id}Name`}
              type="text"
              label="User Name"
              placeholder="Enter User Name"
              onChange={this.handleChangeName}
              value={this.state.name}
            />
            <FieldGroup
              role="form"
              id={`${this.props.id}Nickname`}
              type="text"
              label="User Nickname"
              placeholder="Enter User Nickname"
              onChange={this.handleChangeNickname}
              value={this.state.nickname}
            />
            <Button className="btn btn-primary btn-large centerButton" type="submit">Send</Button>
          </Form>
        );
      }
    }

    export default connect(state => ({
      current_user: state.current_user
    }))(UserUpdateForm);

登录/注册表单有效,但 UserUpdate 无效。实际上,onSubmit 不起作用。也跳过了空格。我没有看到 console.log(url) 或错误。

我认为,问题出在react-bootstrap&lt;Navbar&gt; 组件中,因为当我编写自己的&lt;Modal&gt; 并将其插入此&lt;Navbar&gt; 时,问题并未解决。如果我将 &lt;form&gt;&lt;Navbar&gt; 移出,所有工作。

应用程序:https://on-money.herokuapp.com

Git:https://github.com/yashka713/on_money_front/blob/deploy/src/forms/UserUpdateForm.js

登录:onioni@example.com

密码:password

您应该点击User profile,然后点击FontAwesome Icon。 谢谢 :)

【问题讨论】:

  • 如果您要添加方法和操作,那么为什么要阻止函数调用中的默认值?
  • @YashThakur 因为我不想刷新页面。我只想看看console.log 并确保onSubmit 工作。

标签: reactjs react-bootstrap


【解决方案1】:

我找到了一些看起来像解决方案的东西:https://github.com/react-bootstrap/react-bootstrap/issues/3105

我想哭。

【讨论】:

  • 仅链接的答案在 SO 上不被视为有价值。请在此处填写相关信息,在您的回答中。
猜你喜欢
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多