Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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 Can';t使用react取消选中复选框_Javascript_Reactjs_Jsx - Fatal编程技术网

Javascript Can';t使用react取消选中复选框

Javascript Can';t使用react取消选中复选框,javascript,reactjs,jsx,Javascript,Reactjs,Jsx,此复选框被永久选中,我希望预选复选框更改布尔状态。我目前正在使用这个handleChange方法来处理文本输入。我是否必须创建另一个方法来处理复选框,或者是否可以添加到现有方法中 state = { billingEmail:'', billingAddressSame: true, } 与公司地址相同 将手柄更改功能更改为 handleChange = input => e => { this.setState({[input]: !this.state[inpu

此复选框被永久选中,我希望预选复选框更改布尔状态。我目前正在使用这个
handleChange
方法来处理文本输入。我是否必须创建另一个方法来处理复选框,或者是否可以添加到现有方法中

state = {
  billingEmail:'',
  billingAddressSame: true,
}

与公司地址相同

手柄更改功能更改为

handleChange = input => e => {
   this.setState({[input]: !this.state[input]})
}

handleChange
功能更改为

handleChange = input => e => {
   this.setState({[input]: !this.state[input]})
}

您可以通过单个方法控制复选框和输入

构造函数和句柄更改函数-

constructor(props) {
 super(props);
 this.state = {
  isGoing: true,
  numberOfGuests: 2
 };

 this.handleInputChange = this.handleInputChange.bind(this);
}

handleInputChange(event) {
 const target = event.target;
 const value = target.type === 'checkbox' ? target.checked : target.value;
 const name = target.name;

 this.setState({
   [name]: value
 });
}
在渲染函数中-

render() {
 return (
  <form>
    <label>
      Is going:
      <input
        name="isGoing"
        type="checkbox"
        checked={this.state.isGoing}
        onChange={this.handleInputChange} />
    </label>
    <label>
      Number of guests:
      <input
        name="numberOfGuests"
        type="number"
        value={this.state.numberOfGuests}
        onChange={this.handleInputChange} />
    </label>
  </form>
 );
}
render(){
返回(
将要:
嘉宾人数:
);
}

您可以通过一种方法控制复选框和输入

构造函数和句柄更改函数-

constructor(props) {
 super(props);
 this.state = {
  isGoing: true,
  numberOfGuests: 2
 };

 this.handleInputChange = this.handleInputChange.bind(this);
}

handleInputChange(event) {
 const target = event.target;
 const value = target.type === 'checkbox' ? target.checked : target.value;
 const name = target.name;

 this.setState({
   [name]: value
 });
}
在渲染函数中-

render() {
 return (
  <form>
    <label>
      Is going:
      <input
        name="isGoing"
        type="checkbox"
        checked={this.state.isGoing}
        onChange={this.handleInputChange} />
    </label>
    <label>
      Number of guests:
      <input
        name="numberOfGuests"
        type="number"
        value={this.state.numberOfGuests}
        onChange={this.handleInputChange} />
    </label>
  </form>
 );
}
render(){
返回(
将要:
嘉宾人数:
);
}

试试这个,让我知道,它对我来说很好

import React, { Component, Fragment } from 'react';

class SignUp extends Component{
  state = {
    billingEmail:'',
    billingAddressSame: true,
  }

  render(){
    return(<Fragment>

      <input className="input" type="email" className="col-sm-12" placeholder="Email" value={this.state.billingEmail} onChange={e => this.setState({billingEmail: e.target.value})}/>

      <label className="col-sm-12" style={{paddingLeft: "0"}}>
      <input type="checkbox" value="CheckBox1" checked={this.state.billingAddressSame} onChange={e => this.setState({ billingAddressSame: !this.state.billingAddressSame })} />
        Same as company address
      </label>
    </Fragment>)
  }
}

export default SignUp;
import React,{Component,Fragment}来自'React';
类注册扩展了组件{
状态={
billingEmail:“”,
billingAddressSame:没错,
}
render(){
返回(
this.setState({billingEmail:e.target.value})}/>
this.setState({billingAddressSame:!this.state.billingAddressSame}}/>
与公司地址相同
)
}
}
导出默认注册;

试试这个,让我知道,它对我来说很好

import React, { Component, Fragment } from 'react';

class SignUp extends Component{
  state = {
    billingEmail:'',
    billingAddressSame: true,
  }

  render(){
    return(<Fragment>

      <input className="input" type="email" className="col-sm-12" placeholder="Email" value={this.state.billingEmail} onChange={e => this.setState({billingEmail: e.target.value})}/>

      <label className="col-sm-12" style={{paddingLeft: "0"}}>
      <input type="checkbox" value="CheckBox1" checked={this.state.billingAddressSame} onChange={e => this.setState({ billingAddressSame: !this.state.billingAddressSame })} />
        Same as company address
      </label>
    </Fragment>)
  }
}

export default SignUp;
import React,{Component,Fragment}来自'React';
类注册扩展了组件{
状态={
billingEmail:“”,
billingAddressSame:没错,
}
render(){
返回(
this.setState({billingEmail:e.target.value})}/>
this.setState({billingAddressSame:!this.state.billingAddressSame}}/>
与公司地址相同
)
}
}
导出默认注册;

defaultChecked
defaultChecked
这会将我的所有状态更改为布尔值。我仍然需要在其他州存储字符串。如果我使用两种不同的方法,这可能会起作用。这会将我的所有状态更改为布尔值。我仍然需要在其他州存储字符串。如果我使用了两种不同的方法,这可能会起作用。不幸的是,存在相同的问题,无法取消选中复选框。我已编辑了上述代码,但您仍面临相同的问题请告诉我不幸的是,存在相同的问题,无法取消选中复选框。我已编辑了上述代码,但您仍面临相同的问题请告诉我