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..

希望这有助于你..

智能推荐

注意!

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



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

赞助商广告