通过Ruby On Rails中的jquery的onclick函数传递对象的id [英] Passing id of an object through an onclick function of jquery in Ruby On Rails
问题描述
我有一个Controller VendorController
。并且视图文件是 index.html.erb
I have one Controller VendorController
. And view file is index.html.erb
我想通过onclick函数传递供应商的id。
这是我的代码
I want to pass the id of vendor through onclick function. Here is my code
vendors_controller.rb下的代码
def index
@vendor = Vendor.all
end
的代码index.html.erb
<script language="javascript" type="text/javascript">
function func1(id) {
alert(_id);
}
function func2(id) {
alert(id);
}
</script>
<% @vendors.each do |vendor| %>
<tr>
<td><%=link_to vendor.name , '#', onclick: "func1(1)", remote: true %></td>
<td><%= link_to vendor.id, '#', :onclick => 'func2(vendor.id)' %></td>
</tr>
<% end %>
对于 func1()
我得到了警报。但对于 func2
我没有收到任何回复。
请帮助..提前谢谢。
For func1()
i am getting the alert. But for func2
I am not getting any response.
Please help..thank you in advance.
推荐答案
我认为你只需要逃避onclick的内容要在其中包含实际的id,而不仅仅是一个字符串vendor.id - 记住它只是一个字符串,因此当它在浏览器中加载时需要有正确的数据。
I think you just need to escape the contents of your onclick to have the actual id in it, instead of just a string saying "vendor.id" - remember it's just a string, so it needs to have the right data in it when it gets loaded in the browser.
尝试
<td><%= link_to vendor.id, '#', :onclick => "func2(#{vendor.id})" %></td>
注意双引号,以允许字符串插值。
Note double quotes, to allow string interpolation.
这篇关于通过Ruby On Rails中的jquery的onclick函数传递对象的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!