Javascript 如何在下面的字段中自动生成所选选项ID?
实际上,我想用仓库ID保存仓库,所以我决定使用select选项选择任何仓库,它将自动检测其ID并填充下面的输入字段 我正在使用axios发送数据,我已经在后端创建了一个表来存储仓库id,在postman中,我必须手动添加仓库id,并且工作正常。现在我只想自动添加仓库id 因为创建一个用户必须输入手动id的字段是一件愚蠢的事情Javascript 如何在下面的字段中自动生成所选选项ID?,javascript,reactjs,react-native,react-redux,axios,Javascript,Reactjs,React Native,React Redux,Axios,实际上,我想用仓库ID保存仓库,所以我决定使用select选项选择任何仓库,它将自动检测其ID并填充下面的输入字段 我正在使用axios发送数据,我已经在后端创建了一个表来存储仓库id,在postman中,我必须手动添加仓库id,并且工作正常。现在我只想自动添加仓库id 因为创建一个用户必须输入手动id的字段是一件愚蠢的事情 const [store, setStore] = useState([]); const { name, area, lat, long, wh } =
const [store, setStore] = useState([]);
const { name, area, lat, long, wh } = store;
const onChange = e => {
setStore({ ...store, [e.target.name]: e.target.value });
};
const onSubmit = e => {
e.preventDefault();
const config = {
headers: {
"Content-Type": "application/json"
}
};
axios.post("/api/store", store, config);
setStore({
name: "",
area: "",
lat: "",
long: "",
wh: "",
warehouse: "" //field where i want to auto-genrate the warehouse id
});
};
{warehouses.map(ware=>(
//console.log(ware.name),
{ware.name}
))},
请帮助我找到我已经尝试了很多次的解决方案,访问了几乎所有教程,但我没有找到与我的教程相关的解决方案您可以添加
仓库id
状态来设置所选仓库id
const [ warehouseId, setWarehouseId ] = useState("")
onSubmit = e => {
setStore({
...
warehouse: warehouseId // use warehouseId state variable
})
}
onChange = e => {
setStore(...)
setWarehouseId(e.target.id) // use selected option id
}
// Select options
<option key={ware._id} id={ware._id}>{ware.name}</option> // add id attribute
// Input
<input
...
value={warehouseId} // update value attribute
/>
const[warehouseId,setWarehouseId]=useState(“”)
onSubmit=e=>{
固定存储({
...
仓库:warehouseId//使用warehouseId状态变量
})
}
onChange=e=>{
设置存储(…)
setWarehouseId(e.target.id)//使用所选选项id
}
//选择选项
{ware.name}//添加id属性
//输入
sir实际上,当我选择warehouse时,我希望获得与下面在前端给出的仓库id相同的仓库id{“\u id”:“5d63c113ba6e6b2078dfa4f5”,“名称”:“芒果仓库”,“区域”:“芒果”,“lat”:“22.824780”,“长”:“86.211755”,“_uv”:0},```这是我的仓库文档架构
const [ warehouseId, setWarehouseId ] = useState("")
onSubmit = e => {
setStore({
...
warehouse: warehouseId // use warehouseId state variable
})
}
onChange = e => {
setStore(...)
setWarehouseId(e.target.id) // use selected option id
}
// Select options
<option key={ware._id} id={ware._id}>{ware.name}</option> // add id attribute
// Input
<input
...
value={warehouseId} // update value attribute
/>