【问题标题】:Cannot read property forms of undefined无法读取未定义的属性形式
【发布时间】:2018-11-22 10:44:35
【问题描述】:

我有一个 react 表单,而不是 redux 表单,我希望自动提交表单。这是我的代码:

class DummyForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = { value: '' };

    this.handleChange = this.handleChange.bind(this);
    this.onLoadEvent = this.onLoadEvent.bind(this);
  }

  componentWillMount() {
    this.onLoadEvent();
  }

  onLoadEvent() {
    this.document.forms.threedfrom.submit();
  }

  handleChange(event) {
    this.setState({ value: event.target.value });
  }

  render() {
    return (
      <body>
      <form
        name="threedfrom"
        action={this.props.acsUrl}
        method="POST"
      >
          <textarea
            style={{ display: 'none' }}
            name="PaReq"
            value={this.props.pareqMessage}
          />
        <input
          type="hidden"
          name="MD"
          value={this.props.reservationCode}
        />
        <input type="submit" value="Submit" />
      </form>
      </body>
    );
  }
}

但是当组件被挂载时,我得到了错误 Uncaught TypeError: Cannot read property 'forms' of undefined 意味着文档未定义。这怎么可能,我错过了什么?非常感谢

【问题讨论】:

  • 应该在哪里定义this.document
  • 我认为this.document 的意思只是document
  • 我想用 formnamethreedfrom 参与 dom。我实现这个的方式错了吗?
  • 或者我应该用另一种方式实现它;
  • 为什么不直接调用提交时调用的函数呢?模拟提交事件主要是为了编写测试用例。

标签: javascript forms reactjs components onload


【解决方案1】:

this.document 表示文档是此类的成员。您可以像以前一样使用 ref 进行提交

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-13
    • 2015-05-30
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多