gpt4 book ai didi

ruby-on-rails - 如何使用选择菜单和 link_to 重定向到另一个页面 - Ruby on Rails

转载 作者:行者123 更新时间:2023-12-01 06:58:30 24 4
gpt4 key购买 nike

我想使用选择菜单重定向到另一个页面,但是当我选择“主页”时,它不会将我重定向到主页。没发生什么事...

<select>
<option>Select a menu</option>
<option><%= link_to 'Home', '/' %></option>
</select>

感谢您的帮助。

最佳答案

您不能在 SELECT 标签内放置链接。这是 HTML 的限制,而不是 Rails。

你应该做的是这样的:

<%= select_tag(:menu_select, options_for_select([ 'Home', '/' ])) %>

Then you need to ensure that when the new link is selected, the page is loaded.这可以通过 jQuery 使用这样的东西来完成:
$('#menu_select').bind('change', function() { window.location.pathname = $(this).val() });

当表单选择改变时,新的 URL 被加载。

关于ruby-on-rails - 如何使用选择菜单和 link_to 重定向到另一个页面 - Ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2944977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com