首页 > 解决方案 > 复选框不改变值

问题描述

以下不会将复选框的值更改为true或false,它似乎卡在true上。

因此,即使用户不想同意条款,它也会使用户看起来没有取消单击复选框(是的,复选框确实删除了勾选)

我想知道我需要在下面进行什么更改才能更新 const。

const [state, setState] = React.useState({
    marriedstatus: '',
    employmentstatus: '',
    dependants: '',
    income:'',
    firsthomebuyer:'',
    interest:'',
    whentobuild:'',
    firstname: '',
    lastname:'',
    email:'',
    mobile:'',
    state:'',
    suburb:'',
    country:'',
    besttimetocall:'',
    agree: false,
  });




const handleChange = name => event => {
    setState({
      ...state,
      [name]: event.target.value,
    });
  };


 <Grid item lg={6} xs={12}> 
            <Controller as={<FormControl variant="outlined" fullWidth="true">
            <InputLabel ref={inputLabel} htmlFor="agree">
            Agree
            </InputLabel><Checkbox
        name="agree"
        value={state.agree}
        checked={true} 
        onChange={handleChange('agree')}
        inputProps={{ 'aria-label': 'agree' }}
/></FormControl>} name="Agree" control={control} />
        </Grid>

标签: reactjscheckboxmaterial-ui

解决方案


您已经checked={true}为您的复选框进行了硬编码。


推荐阅读