简体   繁体   English

提交 react-bootstrap 表单时的奇怪行为

[英]Weird behaiviour onsubmit react-bootstrap form

I am using react-bootstrap but I am noticing some weird behavior on form submit.我正在使用 react-bootstrap,但我注意到表单提交时出现了一些奇怪的行为。

I have a sign-in file which is a form in a modal我有一个登录文件,它是一种模态形式

        <Modal>  
        ...
          <Form
            className="d-flex justify-content-center align-items-center flex-column"
            onSubmit={onSubmitSignIn}
          >
            <Form.Group controlId="formBasicEmail">
              <Form.Control
                type="email"
                placeholder="Enter email"
                className={`signInputStyle ${
                  invalidEmail ? "invalidFieldStyle" : ""
                }`}
                value={email}
                onChange={handleEmailValidation}
                required
              />
              <div>
                <small className="errorFormText">
                  {invalidEmail ? "must be a valid email" : ""}
                </small>
              </div>
            </Form.Group>
           ...
            <div className="signupAcc">
              Do not have an account?
              <span onClick={() => setModalShow(true)}>SignUp</span>
              <SignUp show={modalShow} onHide={() => setModalShow(false)} />
           </div>
         ...
       </Form>
      </Modal>        

In the sign-in component when you click on the SignUp span, it displays the second modal(which is for sign-up, therefore I have two modals now).在登录组件中,当您单击 SignUp 跨度时,它会显示第二个模态(用于注册,因此我现在有两个模态)。

The sign-up modal has a form that is similar to that of the sign-in and they both have onSubmit function.注册模式有一个类似于登录的形式,它们都有 onSubmit 函数。

      <Modal>  
        ...
         <Form
            className="d-flex justify-content-center align-items-center flex-column"
            onSubmit={onSubmitValue}
          >
            <Form.Group controlId="formGridFirstName1">
              <Form.Control
                placeholder="First Name"
                required
                name="firstname"
                className={`signInputStyle ${
                  invalidateField.firstname ? "invalidFieldStyle" : ""
                }`}
                onChange={handleUserInput}
              />
              <div>
                <small className="errorFormText">
                  {invalidateField.firstname
                    ? "must be at least three letters"
                    : ""}
                </small>
              </div>
            </Form.Group>
         ...
       </Form>
      </Modal>        

I noticed when I clicked on the button(type='submit') for the sign-up file it also triggers the onSubmit for the sign-in.我注意到当我点击注册文件的按钮(type='submit')时,它也会触发 onSubmit 进行登录。 My question is why is this behavior?我的问题是为什么会出现这种行为? Is it because both form fields are in the dom already?是因为两个表单字段都已经在 dom 中了吗? or something else?或者是其他东西? I really want to know the reason behind this behavior and how to resolve it.我真的很想知道这种行为背后的原因以及如何解决它。 Thank you.谢谢你。

It is just native HTML button behaviour.它只是本机 HTML button行为。 As stated on the documentation , the button element contains the type attribute accepts 3 values: submit , reset , and button .正如文档中所述, button 元素包含type属性,接受 3 个值: submitresetbutton If the type attribute is supplied with the submit value, it will如果type属性与submit值一起submit ,它将

Submit the current form data.提交当前表单数据。

Deep inside the hood of, React-bootstrap's Form Components, it probably makes use of the HTML Form Element, hence clicking on the button triggers the same mechanism for Form.submit() .在 React-bootstrap 的Form组件的深处,它可能使用了 HTML表单元素,因此单击按钮会触发相同的Form.submit()机制。

If you do not want this behaviour, you may simply remove the type attribute ,or set the type attribute aas button ( type="button" ).如果您不想要这种行为,您可以简单地删除type属性,或将 type 属性设置为按钮( type="button" )。

Make one of the buttons as type="button" and add an onClick action.将其中一个按钮设为 type="button" 并添加一个 onClick 操作。 the other one should work this way.另一个应该这样工作。

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

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