[英]Adding an onclick option to link_to method in rails?
我想在link_to方法中添加onclick選項以加載模式對話框...我使用的是Rails版本2.3.8,我在google上搜索,無法做到這一點。 請有人幫我嗎?
我的link_to方法如下。
<%= link_to 'All countries',{:controller=>'countries', :action=>'new'}, :remote => true %>
如果您使用的是2.3.8,則沒有:remote => true。 如果嘗試執行ajax操作,則需要使用link_to_remote。
因此,它看起來像:
<%= link_to_remote 'All countries', :url => {:controller => 'countries', :action => 'new'}%>
<div id="populate_me"></div>
和您的新方法將不得不處理類似的ajax請求
country_controller.rb
def new
<do something>
render :update do |page|
page.replace_html 'populate_me', :partial => 'whatever'
end
end
更新
如果除了ajax操作還需要onclick,則可以將其傳遞到html選項中:
<%= link_to_remote 'All countries', :url => {:controller => 'countries', :action => 'new'}, :html => {:onclick => 'alert("some javascript executed before ajax")'} %>
您可以將其添加到鏈接中:
, :class => "pop light", :id => "modal_link"
然后,您的JS會顯示以下內容:
<script type="text/javascript">
$(document).ready(function() {
$('a.poplight[href^=#]').click(function() {
var popID = $(this).attr('rel'); //Get Popup Name
var popURL = $(this).attr('href'); //Get Popup href to define size
var query= popURL.split('?');
var dim= query[1].split('&');
var popWidth = dim[0].split('=')[1]; //Gets the first query string value
$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"></a>');
$('a.close').hide();
var popMargTop = ($('#' + popID).height() + 80) / 2;
var popMargLeft = ($('#' + popID).width() + 80) / 2;
$('#' + popID).css({
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft
});
$('body').append('<div id="fade"></div>');
$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
return false;
});
$('a.close').live('click', function() {
$('#fade , .popup_block').fadeOut(function() {
$('#fade, a.close').remove();
});
return false;
});
$('#modal_link').click();
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.