【问题标题】:passing js function a value when using Rails button_to_function使用 Rails button_to_function 时向 js 函数传递一个值
【发布时间】:2009-04-24 21:56:06
【问题描述】:

我有一张我称之为资源的表格,希望在用户连续单击按钮时在 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?

【问题讨论】:

    标签: javascript jquery ruby-on-rails


    【解决方案1】:

    应该像下面的代码

    <% 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      相关资源
      最近更新 更多