简体   繁体   English

如何通过选择 select 标签中的某个选项将 URL 获取到页面

[英]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.

相关问题 如何从一个jsp页面获取选项标签名称以选择另一个jsp页面的标签 - how to get option tag name from one jsp page to select tag of another jsp page 如何使用jquery从select标签中的选定选项中获取类名警报? - How to get alert of class name from selected option in select tag using jquery? 如何从“选择”标签中获取所选项目? - How to get the selected item from a 'select' tag? 如何设置 SELECT Tag 的默认选项以及如何获取所选选项的索引? - How to set the default option for SELECT Tag and how to get the Index of the selected option? 如何使用javascript从select标记中获取所选值,然后使用该值转到另一页 - How to get selected value from select tag using javascript and go to another page using that value 如何在页面加载时获取 URL 参数并设置 select 选项的值? - how to get a URL parameter and set the value of a select option on page load? 渲染从jQuery Ajax中选择Tag,然后从选定的选项中获取值 - Render select Tag from jQuery Ajax , then get the value from selected option 如何使用JQuery / JSon从选择选项中获取选择的项目 - How to get selected item from select option using JQuery/JSon 如何从 HTML 选择中获取选定的选项数据属性 - How to get selected option data attribute from HTML select 如何从select元素中获取所选选项的文本? - How to get text of a selected option from the select element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM