Material UI (React.js) 中的函数调用未被调用
Function call in Material UI (React.js) not getting called
- 今天开始 Material UI 不知道为什么这个功能
handelSubmit() 未被调用。控制台功能上没有任何内容未被调用。不知道我在做什么
打错了,谢谢。
- 我正在从 form-tag 调用该函数,也尝试从
在外面,但它不起作用。
import React, { useState } from 'react';
import Typography from '@material-ui/core/Typography';
import { Button, Container } from '@material-ui/core';
import { TextField } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
const useStyle = makeStyles({
field: { marginTop: 20, marginBottom: 20, display: 'block' },
});
export default function Create() {
const classes = useStyle();
const [name, setName] = useState('');
const [phnNO, setPhnNo] = useState('');
const [address, setAddress] = useState('');
const [nameError, setnameError] = useState(false);
const [phnNoError, setphnNOError] = useState(false);
const handleSubmit = (e) => {
e.preventDefault();
setnameError(true);
setphnNOError(true);
if (name == '') {
setnameError(true);
}
if (phnNO == '') {
setphnNOError(true);
}
if (name && phnNO && address) {
console.log(name, phnNO, address);
}
};
return (
<form noValidate autoComplete="off" onSubmit={handleSubmit}>
<TextField
onChange={(e) => setName(e.target.value)}
className={classes.field}
label="Your Name"
variant="outlined"
color="secondary"
fullWidth
required
error={nameError}
/>
<TextField
onChange={(e) => setPhnNo(e.target.value)}
className={classes.field}
label="Your mobile number"
variant="outlined"
color="secondary"
fullWidth
required
error={phnNoError}
/>
<TextField
onChange={(e) => setAddress(e.target.value)}
className={classes.field}
label="Your address"
multiline
rows={4}
variant="outlined"
color="secondary"
fullWidth
/>
<Button
className={classes.buttn}
variant="outlined"
color="primary"
align="left">
Register
</Button>
</form>
);
}
您需要将按钮类型设置为 submit
才能使用该按钮进行实际的表单提交。
<Button
className={classes.buttn}
type="submit"
variant="outlined"
color="primary"
align="left"
>
Register
</Button>
- 今天开始 Material UI 不知道为什么这个功能 handelSubmit() 未被调用。控制台功能上没有任何内容未被调用。不知道我在做什么 打错了,谢谢。
- 我正在从 form-tag 调用该函数,也尝试从 在外面,但它不起作用。
import React, { useState } from 'react';
import Typography from '@material-ui/core/Typography';
import { Button, Container } from '@material-ui/core';
import { TextField } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
const useStyle = makeStyles({
field: { marginTop: 20, marginBottom: 20, display: 'block' },
});
export default function Create() {
const classes = useStyle();
const [name, setName] = useState('');
const [phnNO, setPhnNo] = useState('');
const [address, setAddress] = useState('');
const [nameError, setnameError] = useState(false);
const [phnNoError, setphnNOError] = useState(false);
const handleSubmit = (e) => {
e.preventDefault();
setnameError(true);
setphnNOError(true);
if (name == '') {
setnameError(true);
}
if (phnNO == '') {
setphnNOError(true);
}
if (name && phnNO && address) {
console.log(name, phnNO, address);
}
};
return (
<form noValidate autoComplete="off" onSubmit={handleSubmit}>
<TextField
onChange={(e) => setName(e.target.value)}
className={classes.field}
label="Your Name"
variant="outlined"
color="secondary"
fullWidth
required
error={nameError}
/>
<TextField
onChange={(e) => setPhnNo(e.target.value)}
className={classes.field}
label="Your mobile number"
variant="outlined"
color="secondary"
fullWidth
required
error={phnNoError}
/>
<TextField
onChange={(e) => setAddress(e.target.value)}
className={classes.field}
label="Your address"
multiline
rows={4}
variant="outlined"
color="secondary"
fullWidth
/>
<Button
className={classes.buttn}
variant="outlined"
color="primary"
align="left">
Register
</Button>
</form>
);
}
您需要将按钮类型设置为 submit
才能使用该按钮进行实际的表单提交。
<Button
className={classes.buttn}
type="submit"
variant="outlined"
color="primary"
align="left"
>
Register
</Button>