簡體   English   中英

為什么我提交表單時function沒有執行?

[英]Why is the function not implemented when I submitted the form?

我傳遞給事件的function在表單提交時沒有執行

import React from 'react';
import styles from './Form.module.css';

import Button from './Button';

const Form = function (props) {

  const addUserHandler = function (e) {
    e.preventDefault();
    console.log(135);
  };

  return (
    <form onSubmit={addUserHandler}>
      <input
        type='text'
        className={styles.input}
        placeholder='Your Age'
        ref={nameInputRef}
      />

      <Button type='submit'>add user</Button>
    </form>
  );
};

export default Form;

我預計 function 將在我提交表單時執行。

您沒有將type<Button>正確傳遞到<button> ,這意味着submit按鈕呈現為不提交表單的普通按鈕。 代替:

<button type={props.type ? 'button' : props.type}>

做:

<button type={props.type ? props.type : 'button'}>

或更短:

<button type={props.type || 'button'}>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM