如何在我的视图文件中使用link_to从我的javascript文件调用函数?
我的脚本如下:
var toLeft=function()
{
var sel = document.getElementById("#right");
var lin = document.getElementById("#toleft");
var len = sel.options.length;
var w_controller = lin.w_controller;
var w_id = lin.w_id;
var w_type = lin.w_type;
var w_command = lin.w_command;
alert('call works');
for (var i = 0; i < len; i++)
{
if(sel.options[i].selected)
{
$.ajax(
{
type: w_type,
url: w_controller+'/'+w_id+'/'+w_command,
parameters: {to_find_id: sel.options[i].value},
success:function()
{
//Do something on success
alert('done');
}
});
}
}
}
如何使用一个链接&lt;&lt;调用toLeft还有一些参数,比如w_id等等?
答案 0 :(得分:4)
ERB:
<%= link_to 'to left', '#', id: 'to-left' %>
JS:
$(document).ready(function(){
$('#to-left').click(toLeft);
});
答案 1 :(得分:0)
1st you add an html attribute on the link_to:
<% @video.phrases.each do |phrase| %>
<div class = "span4" id ="comment" >
<%= phrase.content %>
</div>
<%= link_to (image_tag("yourimage.png", :size=> "32x32" ), html => {:onclick => "$('#youId').html('CONTENTS OF HTML');"} :remote => true %>
<% end %>
来自Ruby的Javascript:
<% @video.phrases.each do |phrase| %>
<div class = "span4" id ="comment" >
<%= phrase.content %>
</div>
<%= link_to (image_tag("yourimage.png", :size=> "32x32" ) :remote => true %>
<% end %>
<script type="text/javascript">
$('a').click(function(){
$("#youId").html('CONTENTS OF HTML');
);
</script>
可能对你有所帮助