2011-06-16 83 views
0

嗨我有tokenInput文档,但不知何故我的text_field category_tokens没有预先填入编辑类别。Jquery tokenInput没有预先填充text_field

以下是代码片段

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

任何指针表示赞赏!

回答

1

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

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

希望它能帮助你。

1

变化

<%= 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 } %> 
0

的Json应返回的对象不是字符串,这将字符串转换成JSON对象

JSON.parse(tokendata)