在我的react网站上,我想获取下拉列表的值并存储选择的值,即如果用户选择Admin value = 1则存储在数据库中。我该怎么办?
<div className="col-lg-12">
<select className="form-control" name="roleId" value={this.state.roleId} onChange={this.handleChange} id="sel" style={{ marginTop: "35px" }} required>
<option> Role</option>
<option value="1">Admin</option>
<option value="2">Teacher</option>
<option value="3">TeamLead</option>
</select>
</div>
以下是我的功能
handleChange = (event: any) => {
this.setState({ [event.target.name]: event.target.value }, () => this.validateName())
}
handleSubmit(event: any) {
event.preventDefault();
const { password, confirmPassword, name } = this.state
if (password !== confirmPassword) {
alert('passwords do not match')
}
else {
axios.post('https://localhost:44310/api/Users/', this.state)
.then((response: any) => {
console.log(response);
alert('User has been added successfully')
})
.catch(function (error: any) {
console.log(error);
alert('Sorry! Your request can not be processed at the moment')
});
}
}
答案 0 :(得分:0)
handleChange = (event: any) => {
this.setState({ roleId: event.target.value }, () => this.validateName()) }
我假设不管用户选择哪种选项,都需要将拖放的名称设为roleId
。
此更改完成后,您将在this.state.roleId
中拥有所选角色