简体   繁体   English

用AJAX替换div的内容

[英]Replacing content of a div with AJAX

I have this div 我有这个div

<div class="right-content"> </div>

in which I load all the data I want to show via AJAX. 在其中加载我想通过AJAX显示的所有数据。
I have loaded some html in it which has a link like 我已经在其中加载了一些html链接

<%= link_to job_applicants_path(j.id), :class => "applicants-link" do %>
    <span class="badge"><%= cantSolic (j.id) %></span>
<% end %>

And when I click on that link, I'd like to remove the previous content of the right-content div and load the new html from job_applicants' view, but it's loading it like a new page. 当我单击该链接时,我想删除正确内容div的先前内容,并从job_applicants的视图中加载新的html,但是它像新页面一样加载。 How can I do this? 我怎样才能做到这一点?

This is the js I'm using: 这是我正在使用的js:

$(".applicants-link").click(function(){
    var link = $(this);
    var path = link.attr("href");
    if (path != null){
        $.ajax({
            url: path,
            async: true,
            success: function(result){
                $(".right-content").html(result);
            },
            error: function(result){
                $(".right-content").html ("<h4>No se ha podido cargar la información solicitada. Intentelo más tarde.</h4>")
            }
        })
    return false;
    } 
});
  • 1 , the data that is coming from another page , is getting loaded into that <div class="right-content"> which you are calling it as 'Entire Page'. 1,来自另一个页面的数据,被加载到<div class="right-content"> ,您将其称为“整个页面”。

  • 2 , you can place a <div> giving it an id on that page(to be fetched) , then make a simple ajax request to load the content. 2,您可以在要获取的页面上放置一个<div>为它提供一个ID,然后进行简单的ajax请求以加载内容。

    $('#currentdiv').load('page.asp #div_to_load'); $('#currentdiv')。load('page.asp #div_to_load');

It will load the content of #div_to_load from another page into #currentdiv of your current page. 它将#div_to_load的内容从另一个页面加载到当前页面的#currentdiv中。

Rails has a way to handle this without writing on click functions and $ajax calls. Rails提供了一种无需编写click函数和$ ajax调用即可处理此问题的方法。 Change your link so that rails knows that you want to send an ajax request by adding :remote => true : 更改链接,以便Rails通过添加:remote => true来知道您要发送ajax请求:

<%= link_to job_applicants_path(j.id), :class => "applicants-link", :remote => true do %>
<span class="badge"><%= cantSolic (j.id) %></span>

Then in your controller (I am assuming job_applicants_path is routed to a "show" action in your controller) respond to the ajax request and do anything specific to an ajax request: 然后在您的控制器中(我假设job_applicants_path被路由到您控制器中的“ show”操作)来响应ajax请求并执行特定于ajax请求的任何操作:

def show
  #your logic for all requests types here
  @some_variable = "abc123"
  respond_to do |format|
    format.html {}
    format.js { #do any ajax specific stuff here }
  end
end

Rails will then send the contents of file show.js.erb to be executed as JS, so you can put something like this in it: 然后,Rails将发送文件show.js.erb的内容以JS的形式执行,因此您可以在其中添加以下内容:

$(".right-content").html("<%= escape_javascript(render(partial: 'name_of_your_partial_to_render', locals: { some_variable: @some_variable}))%>")

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

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