我有一个用ReactJS开发的应用程序,并且我正在使用一个选择组件来获取选项列表。代码如下:
<Select
name="planting_system_id"
id="planting_system_id"
options={plantingSystemsList}
value={plantingSystemsList.find(e => e.label === planting_system_description)}
placeholder="Select..."
isDisabled={apiData ? false : true}
onChange={(event) => this.onChangeInputSelected("planting_system_id", event)}
/>
但是,我无法使用通过querySelector读取参数的函数来获取这些值
receiveFormData() {
this.setState({ planting_system_id: document.querySelector("#planting_system_id").value });
}
当我单击表单上的submit按钮时,将调用receiveFormData函数,但无法获得planting_system_id的值。
值得一提的是,此表单是为更新数据而呈现的页面的一部分。然后,在加载时,它用数据库中的值更新它的字段。
发布于 2021-05-04 12:04:53
您应该使用refs
:https://pl.reactjs.org/docs/hooks-reference.html#useref
const plantingSystemRef = useRef(null);
<Select
ref={plantingSystemRef}
options={plantingSystemsList}
value={plantingSystemsList.find(e => e.label === planting_system_description)}
placeholder="Select..."
isDisabled={apiData ? false : true}
onChange={(event) => this.onChangeInputSelected("planting_system_id", event)}
/>
https://stackoverflow.com/questions/67384385
复制