更改所选选项时,选择值不会更改
Posted
技术标签:
【中文标题】更改所选选项时,选择值不会更改【英文标题】:Select value doesn't change when the selected option is changed 【发布时间】:2021-08-24 17:54:43 【问题描述】:我需要一个向下滚动条来选择不同的选项,并且在选择任何选项时,它的值应该出现在该条中。现在的问题是,值不会根据选择而改变:
const [stateSelected, setStateSelected] = useState('')
const stateChange = e =>
setStateSelected(e.target.value)
var states = statepicker.getStates('us');
return (
<select className="settings__inputSelect" onChange=stateChange value=''>
<option value="" disabled=true>Select State</option>
states.map(state => (
<option value=state name="state">state</option>
))
</select>
);
【问题讨论】:
有效!请把它放在答案中,让我奖励你 这很好,但我相信以前有人问过这个问题。 这能回答你的问题吗? Why can't I change my input value in React even with the onChange listener Other related questions 【参考方案1】:const [stateSelected, setStateSelected] = useState('');
const stateChange = e =>
setStateSelected(e.target.value)
var states = statepicker.getStates('us');
return (
<select className="settings__inputSelect" onChange=stateChange value=stateSelected>
<option value="" disabled=true>Select State</option>
states.map(state => (
<option value=state name="state">state</option>
))
</select>
);
【讨论】:
以上是关于更改所选选项时,选择值不会更改的主要内容,如果未能解决你的问题,请参考以下文章