简体   繁体   English

使用Rails的jQuery标签:替换标签内容

[英]JQuery tabs with Rails: replacing tab content

I have a tab with a page rendered inside it. 我有一个选项卡,里面有一个页面。 Everything is good. 万事皆安。 Then the user clicks a button. 然后用户单击一个按钮。 I want the output of doing that to show up inside the tab. 我希望这样做的输出显示在选项卡中。 Currently it takes the user off the tabs view entirely. 当前,它使用户完全脱离标签视图。

Here's the tab html in the .html.erb file: 这是.html.erb文件中的html标签:

<div id="tabs">
  <ul>

    <li><a href="urls">Manage URLs</a></li>
    ...other tabs...
  </ul>

  <div id="urls-tab">
    <p>
    </p>
  </div>
  ...other tabs...
</div>

I have a new button on a form as such: 我在这样的表单上有一个新按钮:

<%= form_tag('urls/someaction_not_new', :method => :post, :id = "top-form") do %>
  ...content...
  <input type="button" id="New" value="New" onclick="someJavascript()">

<% end %>

Finally, the javascript: 最后,javascript:

$("#tabs").tabs();

function someJavascript() {
  document.location = "urls/new"
}

I know that the javascript doesn't work, and I never expected it to. 我知道javascript无法正常运行,而且我从未想到过。 What I need to know is how to build that function such that it loads the 'urls/new' output to the tab rather than redirecting to a new view w/o the tabs. 我需要知道的是如何构建该函数,以便将“ urls / new”输出加载到选项卡,而不是重定向到不带选项卡的新视图。

Any help would be deeply appreciated! 任何帮助将不胜感激!

use this : 用这个 :

function someJavascript() {
  $('#tabs').load('urls/new')
}

It will load your page 'urls/new' in #tabs 它将在#tabs中加载页面“ urls / new”

The reason why it is doing that, is because it is a html request, and it refreshes the page which causes your tabs to disappear. 之所以这样做,是因为它是一个html请求,并且刷新了导致您的标签消失的页面。

Looks like you should trigger an AJAX request when your form is submitted. 看起来您应该在提交表单时触发AJAX请求。 If you do that, you could render a javascript response in your view, and render the html appropriately inside the tab. 如果这样做,则可以在视图中呈现javascript响应,并在选项卡内适当地呈现html。

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

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