【问题标题】:Jquery tokenInput not prepopulating the text_fieldJquery tokenInput 未预填充 text_field
【发布时间】:2011-06-16 08:13:53
【问题描述】:

您好,我有 tokenInput 文档,但不知何故,我的 text_field category_tokens 在编辑时没有预先填充类别。

这里是代码sn-ps

<input id="product_category_tokens" type="text" size="30" name="product[category_tokens]" data-pre="[{"created_at":"2010-09-13T03:33:17Z","description":"","id":x,"name":"Kitchen & Dining ","parent_id":xx,"permalink":"kitchen-dining","updated_at":"2011-01-05T11:17:10Z"}]" style="display: none;">

$(function() {
  $("#product_category_tokens").tokenInput("/categories.json", {
      crossDomain: false,
      prePopulate: $('#product_category_tokens').data('pre'),
      preventDuplicates: true
  });
});

<%= f.text_field :category_tokens, "data-pre" => @product.categories.map(&:attributes).to_json %>

感谢任何指针!

【问题讨论】:

    标签: ruby-on-rails jquery jquery-plugins jquery-tokeninput


    【解决方案1】:

    也许您可以尝试将您的prePopulate 更改为$(this).data("pre")

    另一个技巧是在 JS 函数和视图中使用字段类而不是 id,因为它在渲染时会发生变化。

    希望对你有所帮助。

    【讨论】:

      【解决方案2】:

      改变

      <%= f.text_field :category_tokens, "data-pre" => @product.categories.map(&:attributes).to_json %>
      

      <%= f.text_field :category_tokens, input_html => { "data-pre" => @product.categories.map(&:attributes).to_json } %>
      

      【讨论】:

        【解决方案3】:

        Json 应该返回对象而不是字符串,这会将字符串转换为 Json 对象

         JSON.parse(tokendata)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多