2011-06-16 84 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)