jQuery不能在第一次點擊時工作

[英]jQuery not works on first click


I am using Rails and I am trying to create a bookmark icon in ajax. The code works, the problem is on the first click the request is sent but the image doesn't change, for the next clicks the behavior is OK (request sent and image change).

我正在使用Rails,我正在嘗試在ajax中創建書簽圖標。代碼工作,問題是第一次點擊請求發送但圖像沒有改變,因為下次點擊行為是OK(請求發送和圖像更改)。

My view:

我的看法:

<%= link_to favorite_company_job_path(@job, matching_id: candidate.id), method: :post, remote: true do %>
  <span class="favorite">
    <% if candidate.favorite %>
      <img src="<%= asset_url "favorite-activated.png" %>" alt="favorito" class="img-40" id="favorite_activated_<%= candidate.id %>">
    <% else %>
      <img src="<%= asset_url "favorite.png"%>" alt="favorito" class="img-40" id="favorite_<%= candidate.id %>">
    <% end %>
  </span>
<% end %>

My file favorite.js.erb

我的文件favorite.js.erb

$('#favorite_<%= @matching.id %>').on({
  'click': function(){
    $('#favorite_<%= @matching.id %>').attr('src','<%= asset_url "favorite-activated.png" %>');
    $('#favorite_<%= @matching.id %>').attr('id','favorite_activated_<%= @matching.id %>');
  }
});

$('#favorite_activated_<%= @matching.id %>').on({
  'click': function(){
    $('#favorite_activated_<%= @matching.id %>').attr('src','<%= asset_url "favorite.png" %>');
    $('#favorite_activated_<%= @matching.id %>').attr('id','favorite_<%= @matching.id %>');
  }
});

My controller:

我的控制器:

def favorite
    @matching = @job.matchings.find(params[:matching_id]) 
    if @matching.favorite
      @matching.favorite = false
    else
      @matching.favorite = true
    end
    @matching.save
    respond_to do |format|
      format.html { redirect_to company_job_path(@job) }
      format.js  # <-- will render `app/views/company/jobs/favorite.js.erb`
    end
  end

Any idea?

任何想法?

2 个解决方案

#1


1  

What I see from your code right now, that in your response you just setup listeners for click, while what you actually need to do is just to launch your code. You don't need listeners, because you've already setup them in rails itself:

我現在從你的代碼中看到的是,在你的回復中你只是設置了點擊的聽眾,而你真正需要做的只是啟動你的代碼。您不需要偵聽器,因為您已經在rails本身中設置了它們:

<%= link_to favorite_company_job_path(@job, matching_id: candidate.id), method: :post, remote: true do %>

I think that changing your favorite.js.erb and setup just a part of code that you expect to be a callback:

我認為改變你的favorite.js.erb並設置你期望成為回調的代碼的一部分:

$('#favorite_<%= @matching.id %>').attr('src','<%= asset_url "favorite-activated.png" %>');
$('#favorite_<%= @matching.id %>').attr('id','favorite_activated_<%= @matching.id %>');

$('#favorite_activated_<%= @matching.id %>').attr('src','<%= asset_url "favorite.png" %>');
$('#favorite_activated_<%= @matching.id %>').attr('id','favorite_<%= @matching.id %>');

should work for you. Hope this helps

應該適合你。希望這可以幫助

#2


0  

Instead of $('#id').on('click', function(){});

而不是$('#id')。on('click',function(){});

Use $(document).on('click', '#id', function(){});

使用$(document).on('click','#id',function(){});

Hope this helps you..

希望這有助於你..


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/11/17/7df12eb8dcc444c1d7a159e87502ccc0.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com