2009-08-05 144 views
10

我有這個工作大部分確認。我的軌道鏈接是:Rails的摧毀與jQuery AJAX

<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => 'delete', :confirm => 'Are you sure?', :id => 'trash') %> 

:class => "delete"是調用Ajax的功能,因此,它被刪除,網頁不刷新該工程巨大。但由於頁面不刷新,它仍然存在。所以,我的ID垃圾調用此函數的jQuery:

$('[id^=trash]').click(function(){ 
      var row = $(this).closest("tr").get(0); 
      $(row).hide(); 
      return false; 
}); 

這是隱藏的我點擊任何垃圾桶圖標行。這也很好。我想我已經全部解決了,然後我遇到了這個問題。當您點擊我的垃圾箱時,我會彈出確認框,詢問您是否確定。不管您是否選擇取消或接受,jQuery的火災和它隱藏的行。它不會被刪除,只有在刷新頁面時纔會隱藏。我試圖改變它,這樣的提示是通過jQuery的工作要做,但隨後被鐵軌delete一個行,無論我在我的提示選擇,因爲是被稱爲提示時被調用的函數.destroy。

我的問題確實是我如何獲取取消或接受從鋼軌確認彈出的值,以便在我的jquery中,我可以有一個if語句隱藏,如果他們單擊接受,如果他們單擊取消什麼也不做。

編輯:回答以下問題。 這沒有奏效。我試圖改變我的鏈接:

<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => "delete", :onclick => "trash") %> 

,並把這個在我的jQuery

function trash(){ 
if(confirm("Are you sure?")){ 
    var row = $(this).closest("tr").get(0); 
    $(row).hide(); 
    return false; 
} else { 
//they clicked no. 
} 
} 

但功能從來沒有叫。它只是刪除它沒有提示,並沒有隱藏它。 但是,這給了我一個想法。

我接過阿賈克斯在呼喚

 $('a.delete').click (function(){ 
      $.post(this.href, {_method:'delete'}, null, "script"); 
      $(row).hide(); 
}); 

刪除功能並修改其實現代碼:

刪除:confirm => 'Are you sure?'

$('a.delete').click (function(){ 
     if(confirm("Are you sure?")){ 
      var row = $(this).closest("tr").get(0); 
      $.post(this.href, {_method:'delete'}, null, "script"); 
      $(row).hide(); 
      return false; 
     } else { 
      //they clicked no. 
      return false; 
     } 

}); 

其中的伎倆。

回答

3

可以刪除:

:confirm => 'Are you sure?' 

,並用自定義onclick事件替換它,就像這樣:

<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => 'delete', :onclick => 'someJSFunction', :id => 'trash') %> 

然後,在你的application.js(或任何其他JS文件),這樣做:

function someJSFunction(){ 
    if(confirm("Are you sure?"){ 
    //they clicked yes. 
    } else { 
    //they clicked no. 
    } 
} 

對不起,我沒有時間,現在對其進行測試,但應該工作。

+0

沒有工作,但它使我的答案。 – Mike 2009-08-05 18:24:50

+1

然後給予好評的我樂於助人! ;) – 2009-08-05 18:44:43

12

刪除:確認=>「你確定嗎?「

$('a.delete').click (function(){ 
      if(confirm("Are you sure?")){ 
       var row = $(this).closest("tr").get(0); 
       $.post(this.href, {_method:'delete'}, null, "script"); 
       $(row).hide(); 
       return false; 
      } else { 
       //they clicked no. 
       return false; 
      } 

    }); 
5

謝謝你們,我用下面的教程設置有欄杆的jQuery:

http://www.notgeeklycorrect.com/english/2009/05/18/beginners-guide-to-jquery-ruby-on-rails/連同本教程中使用時

,我用下面的代碼:

 
Query.fn.deleteWithAjax = function() { 
    this.removeAttr('onclick'); 
    this.unbind('click', false); 
    this.click(function() { 
     if(confirm("Are you sure?")){ 
      $.delete_($(this).attr("href"), $(this).serialize(), null, "script"); 
      return false; 
     } else { 
      //they clicked no. 
      return false; 
     } 
    }) 
    return this; 
}; 
3

我有一個類似的問題,但在閱讀這個線程後,我刪除了從我的按鈕確認,並做了以下jQuery:

$('.delete_post').on('click', function(){ if(confirm("Are you sure?")){ $(this).closest('tr').delay().fadeOut(); } else{ return false; } });

而且它似乎爲我做了詭計。我也添加到了我的控制器破壞作用:

respond_to do |format| 
    format.html { render :nothing => true } 
    format.json { head :no_content } 

附:不要忘了補充:遠程=>真到了的link_to

+1

你的意思是:遠程=>真的嗎? – Galaxy 2014-05-09 12:10:01

+0

是啊謝謝 – moon4e 2014-05-09 14:04:42