gpt4 book ai didi

javascript - 使用 Rails button_to_function 时向 js 函数传递一个值

转载 作者:行者123 更新时间:2023-11-30 06:09:28 27 4
gpt4 key购买 nike

我有一个我称之为资源的表,当用户连续单击按钮时,我想在 jquery 对话框中显示特定记录。

在我做的表中:

<td><%=button_to_function 'Show','showresource1()',:class =>"ui-button ui-state-default ui-corner-all", :id => resource.id %></td>

然后在 javascript 中我想将 resource.id 传递给 jQuery 对话框并打开对话框:

<script type="text/javascript" charset="utf-8">
function showresource1()
{
jQuery("#resource_details").load( 'http://localhost:3000/resources/show/4').dialog('open');
}
</script>

我可以打开对话框并加载这个静态 URL,但是我很难将 resource.id 从 button_to_function 向下传递到 js 函数。

button_to_function 是正确的方法吗?如果是,我如何获得在 js 中定义的 URL?

最佳答案

应该是下面的代码

<% for res_id in ids %>
<%=button_to_function "Show", "show_resource('#{res_id}')" %>
<% end %>

<script type="text/javascript" charset="utf-8">
function show_resource(res_id)
{
jQuery("#resource_details").load( 'http://localhost:3000/resources/show/' + res_id).dialog('open');
}
</script>

关于javascript - 使用 Rails button_to_function 时向 js 函数传递一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/787716/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com