Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应-复选框输入未正确更新(由于浅合并)_Javascript_Reactjs - Fatal编程技术网

Javascript 反应-复选框输入未正确更新(由于浅合并)

Javascript 反应-复选框输入未正确更新(由于浅合并),javascript,reactjs,Javascript,Reactjs,我相信由于浅合并,我的代码中有一些错误,特别是我的复选框,因为它的行为很奇怪。有人能就如何修复它提出一些建议吗 这是我设置状态和处理输入更改的方式: state = { form: { firstName: "", lastName: "", email: "", password: "", rememberMe: false } }; handleChange = e => { const { name, value, che

我相信由于浅合并,我的代码中有一些错误,特别是我的复选框,因为它的行为很奇怪。有人能就如何修复它提出一些建议吗

这是我设置状态和处理输入更改的方式:

state = {
  form: {
    firstName: "",
    lastName: "",
    email: "",
    password: "",
    rememberMe: false
  }
};

  handleChange = e => {
    const { name, value, checked } = e.target;
    const isCheckbox = checked === "checkbox";
    this.setState(prevState => ({
      form: {
        // all other key value pairs of form object
        ...prevState.form,

        // update this one specifically
        [name]: isCheckbox ? checked : value
      }
    }));
  };
提交和验证

  validateForm = () => {
    const formInputs = ["firstName", "lastName", "email", "password", "rememberMe"];

    for (let i = 0; i < formInputs.length; i++) {
      const inputName = formInputs[i];

      if (!this.state.form[inputName].length) {
        return false;
      }
    }

    return true;
  };

  handleSubmit = () => {
    if (this.validateForm()) {
      console.log("Success!");
      console.log(this.state);
    } else {
      console.log("Failure!");
    }
  };
validateForm=()=>{
const formInputs=[“firstName”、“lastName”、“email”、“password”、“rememberMe”];
for(设i=0;i{
if(this.validateForm()){
console.log(“成功!”);
console.log(this.state);
}否则{
log(“失败!”);
}
};
以下是我表格的一部分:

   <form
      className="Form"
      onSubmit={e => {
        e.preventDefault();
        this.handleSubmit();
      }}
    >
      <input name="firstName" onChange={this.handleChange} />
      <input name="lastName" onChange={this.handleChange} />
      <input name="email" onChange={this.handleChange} />
      <input name="password" onChange={this.handleChange} />
      <input
        name="rememberMe"
        type="checkbox"
        checked={this.state.form.rememberMe}
        onChange={this.handleChange}
      />
      <button className="no-padding">Submit</button>
    </form>
{
e、 预防默认值();
this.handleSubmit();
}}
>
提交

提交后成功,但我的复选框行为异常,一直处于选中状态。

我认为应该如此

 const { name, value, checked, type } = e.target;
 const isCheckbox = type === "checkbox";


我将代码更改为上面的代码,提交后它给了我一个“失败”错误是什么?堆栈跟踪?您还可以使用name来检查它是否为checkbox。应用程序上没有错误,但表单未能提交(我得到控制台日志“Failure”)。可能是因为没有验证该复选框,所以它被呈现为“Failure”?处理复选框验证的最佳方法是什么?如果它不是必填字段,则可以从验证中排除该字段
 const { name, value, checked } = e.target;
     const isCheckbox = name === "rememberMe";