gpt4 book ai didi

javascript - Jquery Tokeninput 和动态嵌套表单

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:21:28 26 4
gpt4 key购买 nike

我正在使用 R.B. railcast - #197 Nested Model Form Part 2 的设置向表单动态添加字段,但我在获取 Tokeninput fields 时遇到问题去工作。

表格:

<%= form_for(current_user, :url => user_products_path) do |f| %>
<%= f.error_messages %>
<%= f.fields_for :user_products do |builder| %>
<%= render "user_product_fields", :f => builder %>
<% end %>
<%= link_to_add_fields "Add a UserProduct", f, :user_products %>
<%= f.submit "Create" %>
<% end %>

这是我的 user_product_fields 部分, token text_fields 是我遇到的问题:

<div class="fields">
<%= f.label :product_token, "Product" %>
<%= f.text_field :product_token, :id => 'product_token' %>
<%= f.label :address_token, "Address" %>
<%= f.text_field :address_token, :id => 'address_token' %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= link_to_remove_fields "remove", f %>
</div>

我的 application.js 中的 Jquery Tokeninput 函数:

$(function() {
$("#product_token").tokenInput("/products.json", {
prePopulate: $("#product_token").data("pre"),
tokenLimit: 1
});
});

$(function() {
$("#address_token").tokenInput("/business_addresses.json", {
prePopulate: $("#address_token").data("pre"),
tokenLimit: 1
});
});

嵌套形式在函数中的作用是这样的:

function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}

function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}

这里是这一行:

var new_id = new Date().getTime();

使 tokeninput 字段动态化,这是我从 HTML 中提取的内容,请注意字段中不断变化的长数字。这是因为上面的行。

<label for="user_user_products_attributes_1313593151076_product_token">Product</label>
<label for="user_user_products_attributes_1313593146876_product_token">Product</label>
<label for="user_user_products_attributes_1313593146180_product_token">Product</label>

当字段不断变化时,如何让我的 token 字段正常工作?

谢谢。


编辑:新的工作代码。

 function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(content.replace(regexp, new_id)).insertBefore($(link).parent()).trigger("nestedForm:added");
}

$('div.fields').live("nestedForm:added", function() {
$("#product_token", $(this)).tokenInput("/products.json", {
prePopulate: $("#product_token", $(this)).data("pre"),
tokenLimit: 1
});
});

当尝试使用 TokenInput data-pre 时:

def new
@user_product = current_user.user_products.build

# This line below is for TokenInput to work, This allowed me to use @products.map on the form.
@products = []
end

def edit
@user_product = UserProduct.find(params[:id])

# This line below had to find the Product associated with the UserProduct
@products = [@user_product.product]
end

最佳答案

您可以使用 jQuery 的 insertBefore而不是 before因为这将返回插入的元素。它将帮助您触发一些事件。您可以在此事件上有一个监听器,您可以在其中获得 token 输入代码。您还应该使用 class 而不是 id,因为许多元素可以具有相同的 class,但任何两个元素都不应具有相同的 id。

$(content.replace(regexp, new_id)).insertBefore($(link).parent()).trigger("nestedForm:added");

$('div.fields').live("nestedForm:added", function() {
$(".product_token", $(this)).tokenInput("/products.json", {
prePopulate: $(".product_token", $(this)).data("pre"),
tokenLimit: 1
});
});

这只是一个想法,代码没有经过测试。

关于javascript - Jquery Tokeninput 和动态嵌套表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7095616/

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