reactjs复选框的布尔值

时间:2020-06-10 08:32:10

标签: reactjs checkbox react-hooks state

我是reactjs钩子中的新手,在将复选框值(布尔值)发送到状态时遇到困难。我有一个状态,用于存储电子邮件(空字符串),密码(空字符串)和loginTime(布尔值“ true”)。我能够存储电子邮件和密码,但是无法将复选框值存储到状态中。如果我取消选中此复选框,则状态将仅返回布尔值false,而没有参数email和password。

谢谢,抱歉,英语不是我的母语。

检查: https://imgur.com/6VjzJxD

取消选中: https://imgur.com/9nbMzgn

代码如下:

const Login = (props) => {
const [values, setValues] = useState({
    email: "",
    password: "",
    loginTime: true
  });

  const onChange = (event) => {
    setValues({ ...values, [event.target.name]: event.target.value });

  };

  const onSubmit = (e) => {
    e.preventDefault();
    console.log(values)
  };


  return (
    <>
      <section className="main">
        <div className="content-w3ls right">
          <div className="container">
            <div className="header-sign">
              <p>Sign in</p>
            </div>
            <div>
              <form onSubmit={onSubmit} method="post">
                <div className="txtLabel">
                  <label>Email address</label>
                </div>
                <div className="field-group">
                  {/* <label>Email address</label> */}

                  <span className="fa fa-user" aria-hidden="true"></span>

                  <div className="wthree-field">
                    <input
                      name="email"
                      id="email"
                      type="email"
                      // placeholder="Email"
                      required
                      value={values.email}
                      onChange={onChange}
                    ></input>
                  </div>
                </div>
                <div className="txtLabel">
                  <label>Password</label>
                </div>
                <div className="field-group">
                  <span className="fa fa-lock" aria-hidden="true"></span>
                  <div className="wthree-field">
                    <input
                      name="password"
                      id="myInput"
                      type="Password"
                      value={values.username}
                      required
                      onChange={onChange}
                    ></input>
                  </div>
                </div>

                <div className="checklayout">
                  <div>
                    <input type="checkbox" id="check" values={values.loginTime} checked={values.loginTime} onChange={() => setValues(!values.loginTime)}></input>
                    <span className="span-me">Remember Me</span>
                  </div>
                  <div>
                    <div>
                      <a
                        href="#"
                        className="text-right"
                        style={{ textDecoration: "none", fontSize: "14px" }}
                      >
                        Forgot your password?
                      </a>
                    </div>
                  </div>
                </div>

                <div className="wthree-field">
                  <button type="submit" className="btn">
                    {loading ? "Loading..." : "Sign In"}
                  </button>
                </div>
}

export default Login;

1 个答案:

答案 0 :(得分:0)

在复选框的onChange函数中,您没有合并与其他字段一起使用的状态值。应该更新如下

onChange={() => setValues(prev=> ({...prev, loginTime: !prev.loginTime}))}