3

I am trying to retrieve the check value of my checkbox but i get null value this is my code

<form onSubmit={this.onSubmit}>
<fieldset>
<legend>RFX/RFP:</legend>
<div className="form-check">
  <input type="checkbox" className="form-check-input" onChange={this.onChange}  value={this.state.rfx} name="rfx" id="rfx" />
  <label className="form-check-label"  name="rfx">RFX</label>
  &nbsp; &nbsp; &nbsp; &nbsp;

  <input type="checkbox" className="form-check-input" onChange={this.onChange}  value={this.state.rfp}  name="rfp" id="rfp"  />
  
  <label className="form-check-label" >RFP</label>
  &nbsp; &nbsp; &nbsp; &nbsp;

  <input type="checkbox" className="form-check-input" onChange={this.onChange}  value={this.state.rfp_x}  name="rfp(x)" id="rfp(x)"/>
  <label className="form-check-label" >RFP(X)</label>
  &nbsp; &nbsp; &nbsp; &nbsp;


  <input type="checkbox" className="form-check-input"  onChange={this.onChange}name="all" id="all" />
  <label className="form-check-label"  name="all" id="all">ALL</label>
</div>
</form>

and my methods :

constructor(props){
    super(props)
    this.state={
      rfp:"",
      rfx:"",
      rfp_x:"",
      all:"",
    
 
    }
    this.onChange =  this.onChange.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
   }
  onChange(e){
    this.setState({[e.target.name] : e.target.value});}
  onSubmit(e){
    e.preventDefault();
    const FilterRegion={
  
      rfx:this.state.rfx,
      rfp:this.state.rfp,
      rfp_x:this.state.rfp_x,
      all:this.state.all,
    }
    console.log(FilterRegion)
   }

and the screen when i submit my form :

https://gyazo.com/32a24813545ebf7d3a48b72be724a76f

please I try to solve my problem and display the value or the name of my check box! what do I have to do

2 Answers 2

7

When using controlled components, you don't get the value, you simply give it to the HTML: your javascript state is the source of truth. That said, you should make the following changes:

use booleans for checkbox values:

this.state = {
  rfp: false,
  rfx: false,
  rfp_x: false,
  all: false
};

use event.target.checked instead of event.target.value:

this.setState({ [e.target.name]: e.target.checked });

Demo: https://codesandbox.io/s/polished-bush-5mf1n?file=/src/App.js

Sign up to request clarification or add additional context in comments.

2 Comments

If i need to use both "event.target.checked" & "event.target.value" then how can use it?
You can use const { name, checked, value } = event.target and use the name, checked and value variables
1

using react first setState

 var [value,setvalue]=useState(false)

<div className="form-check">
        <input className="form-check-input" checked={value} onChange={(e)=>setValue(e.target.checked)} type="checkbox"  />
       <label className="form-check-label">chechbox value</label>
       {value}
     </div>

note i am using react and bootstrap css

2 Comments

It doesn't work for me, the checkbox stays checked
use onChange={(e)=>setValue(!value)}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.