简体   繁体   English

我们如何通过单击Rails显示不同的表单部分

[英]How can we display different form partials through click in rails

I have a navbar rendering like a partial. 我有一个局部导航栏渲染。

ex: dashboard/menu 例如:仪表板/菜单

In navbar i have a links like mentor, corporate. 在导航栏中,我有一个链接,如导师,公司。

When click on those links, it has render different form partials. 单击这些链接时,它将呈现不同的表单部分。

Ex: 例如:

_menu.html.erb

<a id="mentor" href="dasboard/settings">mentor</a>
<a id="corporate" href="dasboard/settings">corporate</a>

in settings page: mentor and corporate forms within settings page. 在设置页面中:设置页面中的指导者和公司表单。

<div id="men">
<% form_for() %>
  #mentor fields
<% end %>
</div>

<div id="cor">
<% form_for() %>
  #corporate fields
<% end %>
</div>

jquery and css with in settings page: jQuery和CSS在设置页面:

#men,#cor{
 display: none;
}
$("#mentor").on("click", function(){
  $("#men").css("display", "block");
)};
$("#corporate").on("click", function(){
  $("#cor").css("display", "block");
)};

But, noting is happening when i click on navbar links. 但是,当我单击导航栏链接时,正在发生。 Even i did't get any error. 即使我没有任何错误。

updated: 更新:

I placed nav links within settings page. 我在设置页面中放置了导航链接。 Its working fine. 它的工作正常。

But, from menu partial it didn't work. 但是,从菜单部分来看,它不起作用。

You are not selecting properly, there are two mistakes in your jquery sccript 您的选择不正确,jquery脚本中有两个错误

  1. You are not selecting properly. 您选择不正确。 This selector $("men") won't select any thing, because selector will not men until you add # . 这个选择$("men")将不选择任何东西,因为选择不会men ,直到你加#
  2. css({"display: block"}) This is also wrong approach to add css on any element css({"display: block"})这也是在任何元素上添加css的错误方法

Try following 尝试跟随

$("#mentor").on("click", function(){
  $("#men").css("display", "block");
)};
$("#corporate").on("click", function(){
  $("#cor").css("display", "block");
)};

The correct code for your solution would be 解决方案的正确代码是

$("#mentor").on("click", function(event){
  event.preventDefault();
  $("#men").css("display", "block");
});

$("#corporate").on("click", function(event){
  event.preventDefault();
  $("#cor").css("display", "block");
});

Here is the example JSbin 这是JSbin示例

Note: I added the event.preventDefault() to prevent links sending a request to the server, and thus only rendering the loaded code. 注意:我添加了event.preventDefault()来防止链接将请求发送到服务器,从而仅呈现已加载的代码。 If you want to render the form by pulling data from the server, you can use remote links. 如果要通过从服务器提取数据来呈现表单,则可以使用远程链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM