Mik*_*ike 10 jquery ruby-on-rails
我大部分时间都在工作.我的rails链接是:
<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => 'delete', :confirm => 'Are you sure?', :id => 'trash') %>
Run Code Online (Sandbox Code Playgroud)
:class => "delete"正在调用一个ajax函数,以便删除它并且页面不刷新,效果很好.但由于页面没有刷新,它仍然存在.所以我的id垃圾正在调用这个jquery函数:
$('[id^=trash]').click(function(){
var row = $(this).closest("tr").get(0);
$(row).hide();
return false;
});
Run Code Online (Sandbox Code Playgroud)
这隐藏了我点击的任何垃圾图标的行.这也很有效.我以为我把它全部解决了然后我遇到了这个问题.当你点击我的垃圾箱时,我会弹出这个确认框,询问你是否确定.无论你选择取消还是接受,jquery都会触发并隐藏行.它不会被删除,只有在您刷新页面时才会被隐藏.我尝试更改它以便通过jquery完成提示,但是无论我在提示中选择什么,rails都会删除该行,因为在调用提示时正在调用.destroy函数.
我的问题是如何才能获得取消或接受来自rails确认弹出的值,以便在我的jquery中我可以有一个if语句,如果他们单击"接受"则隐藏,如果单击取消则不执行任何操作.
编辑:回答下面的问题.那没用.我尝试将我的链接更改为:
<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => "delete", :onclick => "trash") %>
Run Code Online (Sandbox Code Playgroud)
把它放在我的jquery中
function trash(){
if(confirm("Are you sure?")){
var row = $(this).closest("tr").get(0);
$(row).hide();
return false;
} else {
//they clicked no.
}
}
Run Code Online (Sandbox Code Playgroud)
但这个功能从未被调用过.它只是删除它没有提示,并没有隐藏它.但这给了我一个想法.
我接受了ajax正在调用的删除功能
$('a.delete').click (function(){
$.post(this.href, {_method:'delete'}, null, "script");
$(row).hide();
});
Run Code Online (Sandbox Code Playgroud)
并修改它实现您的代码:
去掉 :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;
}
});
Run Code Online (Sandbox Code Playgroud)
这诀窍.
Mik*_*ike 13
删除:确认=>'你确定吗?'
$('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;
}
});
Run Code Online (Sandbox Code Playgroud)
谢谢大家,我使用以下教程来设置带有rails的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;
};
您可以删除:
:confirm => 'Are you sure?'
Run Code Online (Sandbox Code Playgroud)
并将其替换为自定义 onclick 事件,如下所示:
<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :class => 'delete', :onclick => 'someJSFunction', :id => 'trash') %>
Run Code Online (Sandbox Code Playgroud)
然后,在您的 application.js (或任何其他 JS 文件)中,执行以下操作:
function someJSFunction(){
if(confirm("Are you sure?"){
//they clicked yes.
} else {
//they clicked no.
}
}
Run Code Online (Sandbox Code Playgroud)
抱歉,我现在没有时间测试它,但这应该可行。
| 归档时间: |
|
| 查看次数: |
12393 次 |
| 最近记录: |