简体   繁体   English

是否可以使用按钮单击事件来触发 React 中的表单提交?

[英]Is it possible to use button click event to trigger form submit in React?

Is it possible in React to use the click event on a regular button component to trigger the click of a hidden form submit button/submission of the form with all the form values and updated index intact?是否可以在 React 中使用常规按钮组件上的单击事件来触发隐藏表单提交按钮的单击/表单的提交,并且所有表单值和更新的索引完好无损?

I have an application with a series of steps (like a wizard) and a form in each step that has its own hidden submit button.我有一个包含一系列步骤(如向导)的应用程序和每个步骤中都有自己隐藏提交按钮的表单。 When ButtonNext (and ButtonFinal on the final step) are clicked, is there a way to trigger a click on the hidden form submit button so that onSubmit runs and the form values and index value are then sent to the handleSubmit function?ButtonNext (和最后一步的ButtonFinal )被点击时,有没有办法触发对隐藏表单提交按钮的点击,以便onSubmit运行,然后将表单值和索引值发送到handleSubmit函数?

I can probably access the hidden submit button with a data-key attribute.我可能可以使用data-key属性访问隐藏的提交按钮。

EDIT: ButtonNext and ButtonFinal are direct siblings of the form in the DOM and can't be added to the form.编辑: ButtonNextButtonFinal是 DOM 中表单的直接兄弟,不能添加到表单中。

import React, { useState } from 'react'
import { Row, Col } from 'react-styled-flexboxgrid'
import { Form, ButtonNext, ButtonPrevious, ButtonFinal } from './style'

const Application = ({ steps }) => {
  const [currentStepNum, setCurrentStepNum] = useState(0)
  const [previousStepName, setPreviousStepName] = useState(steps[0].step_name)
  const [nextStepName, setNextStepName] = useState(steps[1].step_name)
  const [formValues, setFormValues] = useState(null)

  const handleSubmit = (values, step) => {
    setFormValues(values)
    setStep(step)
  }

  const handleClick = (event, step) => {
    event.preventDefault()
    setStep(step)
  }

  const setStep = step => {
    step = parseInt(step)
    setCurrentStepNum(step)

    if (step > 0) {
      setPreviousStepName(steps[step - 1].step_name)
    }

    if (step < steps.length - 1) {
      setNextStepName(steps[step + 1].step_name)
    }
  }

  return (
    <>
      <div>
        {steps.map((step, index) => {
          return (
            <div key={index}>
              {currentStepNum == index && (
                <>
                  {step.form.length > 0 && (
                    <Form
                      action="/"
                      fields={fields}
                      onSubmit={(values) => {
                        handleSubmit(values, [index + 1])
                      }}
                    />
                  )}
                  {index > 0 && (
                    <ButtonPrevious
                      onClick={event => {
                        handleClick(event, [index - 1])
                      }}
                    >
                      Back to {previousStepName}
                    </ButtonPrevious>
                  )}
                  {index < steps.length - 1 && (
                    <ButtonNext
                      onClick={Trigger onSubmit here}
                    >
                      Next: {nextStepName}
                    </ButtonNext>
                  )}
                  {index == steps.length - 1 && (
                    <ButtonFinal
                     onClick={Trigger onSubmit here}
                    >
                      FINAL SUBMIT BUTTON PLACEHOLDER
                    </ButtonFinal>
                  )}
                </>
              )}
            </div>
          )
        })}
      </div>
    </>
  )
}

export default Application

If you have a form element with onSubmit set, then any button within that form by default will submit the form.如果您有一个设置了 onSubmit 的表单元素,那么默认情况下该表单中的任何按钮都将提交该表单。 You could disable this default behavior by defining an onClick function for that button, and calling preventDefault() on the passed event.您可以通过为该按钮定义一个 onClick 函数并在传递的事件上调用preventDefault()来禁用此默认行为。

Yes!是的! you can do this wich React in a similar way that plained HTML.你可以用简单的 HTML 类似的方式来做 React。

https://reactjs.org/docs/forms.html https://reactjs.org/docs/forms.html

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

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

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

handleSubmit(event) {
  alert('A name was submitted: ' + this.state.value);
  event.preventDefault();
}

render() {
  return (
    <form onSubmit={this.handleSubmit}>
      <label>
        Name:
        <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
      <input type="submit" value="Submit" />
    </form>
  );
}}

This way the < input type="submit" value="Submit" /> will be your submit button and you just pass the handleSubmit function to form tag, < form onSubmit={this.handleSubmit}>这样< input type="submit" value="Submit" />将是您的提交按钮,您只需将 handleSubmit 函数传递给表单标签, < form onSubmit={this.handleSubmit}>

You can do this in vanilla HTML, which should work in React.你可以在 vanilla HTML 中做到这一点,它应该在 React 中工作。 Clicking the button should automatically trigger the form submission event点击按钮应该会自动触发表单提交事件

<button type="submit">...</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM