[英]How to a get a URL to a page with a certain option from select tag selected
(I'm not asking how to implement this, but how to create a link for a third party site) (我不是问如何实现这个,而是如何为第三方网站创建链接)
Say a site has an element说一个网站有一个元素
<select id="year">
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
The interactions are handled JS/React.交互由 JS/React 处理。
Is it possible to create a URL like https://example.com/#year.option="2022"
that will take people to the page with a certain option selected?是否可以创建一个 URL 之类的
https://example.com/#year.option="2022"
将人们带到选择了某个选项的页面?
there's several ways to achieve this, one way is conditional component rendering when using react:有几种方法可以实现这一点,一种方法是在使用 React 时进行条件组件渲染:
options.map(option => (
if(option.year == 2022){
<Option data={optionDataProps} >
}
))
*make sure you check the query strings in this component *确保检查此组件中的查询字符串
Furthermore you'd have to implement React Router Dom to render the component此外,您必须实施 React Router Dom 来呈现组件
Another Way to achieve is is by doing it serverside in node.js could look somewhat like this:实现的另一种方法是在 node.js 中的服务器端执行它可能看起来有点像这样:
router.get("/api/options", (req, res) => {
const optionYear = req.query.year // "/api/options?year=2022
// then find all options in your DB and return them as JSON for your frontend to receive
})
This is not possible for a third party site, as there is no default browser functionality to achieve this.这对于第三方站点是不可能的,因为没有默认的浏览器功能来实现这一点。 Form values cannot be prefilled from the URL.
无法从 URL 预填表格值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.