1
我正在使用Link_to使用ajax添加一些內容到我的頁面,但我只想讓用戶這樣做一次。我通過用js.erb文件中的新html元素替換鏈接來做到這一點。這是所有工作的罰款如下:Rails link_to 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'}) do %>
<i class="glyphicon glyphicon-plus-sign" title="Expand this branch"></i>
<% end %>
js.erb文件片段替換上面創建的鏈接:
$("#<%[email protected]_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選擇$("#<%[email protected]_id%> > a:has(i)")
沒有找到任何匹配的元素替換。我曾嘗試禁用鏈接作爲js.erb文件中的第一條語句,但如果您很快,仍然可以擊敗它並單擊兩次。有沒有另一種方法可以在點擊後直接禁用鏈接?
更新:的disable_with這樣做,我只是改變了我的jQuery選擇要更換的一個標籤的內容:
$("#<%[email protected]_id%> > a[data-mahi-link-type='expand_task_subset']")
,而不是尋找孩子「我」的標籤,現在所有很棒!
不幸的簡化版,似乎工作。我在某處讀到remote:true會阻止jquery解綁/死亡。我可能需要進一步研究以找到解決方法... – 2015-03-19 07:19:04