无法使用反应状态在变量中获取我的复选框值
Cant get my checkbox value in a variable using state in react
使用状态和处理输入的代码如下。我在 selecting 时无法在控制台中获取我的游戏名称,为什么?
const [tour, setTour] = useState({
cafename: "", contactno: "", state: "", address: "", game: "", date: "", prizepool: ""
});
var name, value;
const handleInputs = (e) => {
console.log(e);
name = e.target.name;
value = e.target.value;
setTour({ ...user, [name]: value });
}
select代码如下。
<div className="form-group">
<label for="games">Games</label>
<select class="custom-select" name= "game" value={tour.game} onChange={handleInputs}>
<option selected>Select game</option>
<option value="leagueoflegends">LEAGUE OF LEGENDS</option>
<option value="valorant">VALORANT</option>
<option value="rainbow6">RAINBOW 6 SIEGE</option>
<option value="dota2">DOTA 2</option>
<option value="cs">CS GO </option>
placeholder="Enter games"
</select>
</div>
CheckBox 已检查 属性 的值:
const handleInputs = (e) => {
name = e.target.name;
value = e.target.type === 'checkbox' ? e.target.checked : e.target.value;
setTour(state => { ...state, [name]: value });
}
使用状态和处理输入的代码如下。我在 selecting 时无法在控制台中获取我的游戏名称,为什么?
const [tour, setTour] = useState({
cafename: "", contactno: "", state: "", address: "", game: "", date: "", prizepool: ""
});
var name, value;
const handleInputs = (e) => {
console.log(e);
name = e.target.name;
value = e.target.value;
setTour({ ...user, [name]: value });
}
select代码如下。
<div className="form-group">
<label for="games">Games</label>
<select class="custom-select" name= "game" value={tour.game} onChange={handleInputs}>
<option selected>Select game</option>
<option value="leagueoflegends">LEAGUE OF LEGENDS</option>
<option value="valorant">VALORANT</option>
<option value="rainbow6">RAINBOW 6 SIEGE</option>
<option value="dota2">DOTA 2</option>
<option value="cs">CS GO </option>
placeholder="Enter games"
</select>
</div>
CheckBox 已检查 属性 的值:
const handleInputs = (e) => {
name = e.target.name;
value = e.target.type === 'checkbox' ? e.target.checked : e.target.value;
setTour(state => { ...state, [name]: value });
}