2009-08-05 142 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