Ruby-on-rails Rails 中的 link_to 方法和单击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/698858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
link_to method and click event in Rails
提问by Yud
How do I create a link of this type:
如何创建这种类型的链接:
<a href="#" onclick="document.getElementById('search').value=this.value">
using method link_toin Rails?
link_to在 Rails 中使用方法?
I couldn't figure it out from Rails docs.
我无法从Rails 文档中弄清楚。
回答by Ron DeVera
You can use link_to_function(removed in Rails 4.1):
您可以使用link_to_function(在 Rails 4.1 中删除):
link_to_function 'My link with obtrusive JavaScript', 'alert("Oh no!")'
Or, if you absolutely need to use link_to:
或者,如果您绝对需要使用link_to:
link_to 'Another link with obtrusive JavaScript', '#',
:onclick => 'alert("Please no!")'
However, putting JavaScript right into your generated HTML is obtrusive, and is bad practice.
但是,将 JavaScript 直接放入生成的 HTML 中是很麻烦的,而且是不好的做法。
Instead, your Rails code should simply be something like this:
相反,您的 Rails 代码应该是这样的:
link_to 'Link with unobtrusive JavaScript',
'/actual/url/in/case/javascript/is/broken',
:id => 'my-link'
And assuming you're using the Prototype JS framework, JS like this in your application.js:
并假设您使用的是Prototype JS 框架,则在您的 JS 中是这样的application.js:
$('my-link').observe('click', function (event) {
alert('Hooray!');
event.stop(); // Prevent link from following through to its given href
});
Or if you're using jQuery:
或者,如果您使用的是jQuery:
$('#my-link').click(function (event) {
alert('Hooray!');
event.preventDefault(); // Prevent link from following its href
});
By using this third technique, you guarantee that the link will follow through to some other page—not just fail silently—if JavaScript is unavailable for the user. Remember, JS could be unavailable because the user has a poor internet connection (e.g., mobile device, public wifi), the user or user's sysadmin disabled it, or an unexpected JS error occurred (i.e., developer error).
通过使用这第三种技术,您可以保证如果 JavaScript 对用户不可用,链接将继续到其他页面——而不仅仅是静默失败。请记住,JS 可能不可用,因为用户的互联网连接不佳(例如,移动设备、公共 wifi)、用户或用户的系统管理员禁用了它,或者发生了意外的 JS 错误(即开发人员错误)。
回答by Jim Morris
To follow up on Ron's answer if using JQuery and putting it in application.js or the head section you need to wrap it in a ready() section...
如果使用 JQuery 并将其放入 application.js 或 head 部分,要跟进 Ron 的回答,您需要将其包装在 ready() 部分中...
$(document).ready(function() {
$('#my-link').click(function(event){
alert('Hooray!');
event.preventDefault(); // Prevent link from following its href
});
});
回答by Alan Wang
just use
只是使用
=link_to "link", "javascript:function()"
回答by Joaquin Diaz
another solution is catching onClick event and for aggregate data to js function you can
另一个解决方案是捕获 onClick 事件并将数据聚合到 js 函数,您可以
.hmtl.erb
.hmtl.erb
<%= link_to "Action", 'javascript:;', class: 'my-class', data: { 'array' => %w(foo bar) } %>
.js
.js
// handle my-class click
$('a.my-class').on('click', function () {
var link = $(this);
var array = link.data('array');
});

