简体   繁体   English

React Form onSubmit 未在功能组件内触发

[英]React Form onSubmit not firing within functional component

I have a form within a functional component called Payment.我在一个名为 Payment 的功能组件中有一个表单。 However when submitting the form by clicking the button the handler handleSubmitPayment(e) doesn't get called.但是,当通过单击按钮提交表单时,不会调用处理程序 handleSubmitPayment(e)。 Nor is there any error message in the console.控制台中也没有任何错误消息。

function Payment() {

  function handleSubmitPayment(e) {
    console.log("Form Submitted");
    console.log(e);
  }


  return (
  ...
  <form onSubmit={(e) => {console.log(e); handleSubmitPayment(e); /*e.preventDefault();*/}}>
  <Row>
      <Col>
        <span className="payment_form_text">CC Number</span>
        <input id="ccnumb" value=""/>
      </Col>
  </Row>
  <Row>
      <Col xs={6}>
       <span className="payment_form_text">CC Exp</span>
       <input id="ccexp" value=""/>
      </Col>
      <Col xs={6}>
          <span className="payment_form_text">CVV</span>
          <input id="cvv" value=""/>
      </Col>
  </Row>
     <Button variant="success" id="payButton" type="submit" style={{ marginTop: 50 }}>
       Pay
     </Button>
 </form>
...
)
}

export default Payment;

Please note these are dummy fields and I am not using a basic form to send raw payment data.请注意,这些是虚拟字段,我没有使用基本表单来发送原始付款数据。

What could be a possible reason for this?这可能是什么原因? Or what code changes do I need to deploy to make this work?或者我需要部署哪些代码更改才能完成这项工作?

I can answer any more questions or provide more details if needed!如果需要,我可以回答更多问题或提供更多详细信息!

I may be misinterpreting what you're asking for here but I believe this is what you want?我可能会误解你在这里的要求,但我相信就是你想要的? All I did was add e.preventDefault() to the handleSubmitPayment function and then in onSubmit I called我所做的只是将e.preventDefault()添加到handleSubmitPayment function 然后在 onSubmit 我调用

<form onSubmit={(e) => handleSubmitPayment(e)}>

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

相关问题 在React中的功能组件中触发事件时是否可以打开另一个功能组件? - Is it possible to open another functional component upon firing an event within a functional component in React? 在功能组件中返回 React 元素 - Returning a React Element within a Functional Component Jest/Enzyme Test 在 React 功能组件中将 Child prop 触发为 function - Jest/Enzyme Test firing Child prop as function in react functional component 表单提交时表单onSubmit函数未触发 - Form onSubmit function not firing when form submitted React 在功能组件中的组件的每次更新时调用构造函数 - React calls constructor on every update for component within functional component 将组件反应到功能组件 - React Component to Functional Component Redux表单卸载组件onSubmit - Redux form unmounting component onSubmit 如何在表单中预填充数据,并且表单可以使用react功能组件进行编辑 - How to prefilled data in form and the form is editable by using react functional component 在 React 中,我可以在另一个功能组件的主体中定义一个功能组件吗? - In React, can I define a functional component within the body of another functional component? React router dom - 表单 onSubmit 将输入值传递给另一个组件并重定向到该组件 - React router dom - Form onSubmit pass input value to another component as well as redirect to that component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM