【发布时间】:2015-03-19 03:25:41
【问题描述】:
我正在使用 Link_to 使用 ajax 向我的页面添加一些内容,但我只希望用户这样做一次。我通过用 js.erb 文件中的新 html 元素替换链接来做到这一点。这一切正常,如下所示:
<%= link_to(expand_task_subset_user_path(user, li_id: li_id, span_id: span_id, user_tasks_to_expand_sym: user_tasks_to_expand_sym),
{:method => :get, remote: true, 'data-mahi-link-type'=>'expand_task_subset'}) do %>
<i class="glyphicon glyphicon-plus-sign" title="Expand this branch"></i>
<% end %>
js.erb 文件 sn-p 替换上面创建的链接:
$("#<%=@span_id%> > a:has(i)").replaceWith("<i class=\"glyphicon glyphicon-minus-sign\" title=\"Collapse this branch\"></i>")
我今天注意到可以在 js.erb 文件中的所有内容完成之前单击该链接两次,这意味着它插入了两次其他内容。我认为解决这个问题的明显方法是像这样添加一个 disable_with :
<%= link_to(expand_task_subset_user_path(user, li_id: li_id, span_id: span_id, user_tasks_to_expand_sym: user_tasks_to_expand_sym),
{:method => :get, remote: true, 'data-mahi-link-type'=>'expand_task_subset', data: { disable_with: 'loading...' }}) do %>
<i class="glyphicon glyphicon-plus-sign" title="Expand this branch"></i>
<% end %>
问题(我认为)是 disable_with 临时更改了 html 并且 jquery 选择器$("#<%=@span_id%> > a:has(i)") 没有找到任何匹配的元素来替换。我也尝试禁用链接作为 js.erb 文件中的第一条语句,但如果你很快,你仍然可以击败它并单击两次。有没有其他方法可以在点击后直接禁用链接?
更新: disable_with 确实替换了“a”标签的内容,所以我只是将我的 jquery 选择器更改为:
$("#<%=@span_id%> > a[data-mahi-link-type='expand_task_subset']")
而不是寻找子“i”标签,现在一切都很好!
【问题讨论】:
标签: jquery ruby-on-rails ajax link-to