javascript - tokenfield 不适用于 jQuery 克隆
问题描述
您好,我正在使用tokenfield创建输入标签。
我试图克隆元素但没有成功。有什么方法可以进行正确的克隆吗?这是一个动态表格。
问题:克隆后字段不起作用。我认为问题在于代币。
这是我的 JavaScript 代码:
$('.tokenfield').tokenfield();
这是我的克隆功能:
$('.clone').on('click',function(){
var newLine = $(".attribute:first").clone();
$("#variants").append(newLine);
});
这是我的 HTML 代码:
<div id="variants">
<div class="row attribute">
<div class="col-lg-4">
<div class="input-group"> <span class="input-group-prepend">
<button class="btn btn-light clone" type="button"><i class="icon-plus3"></i></button>
</span> <input type="text" class="form-control" placeholder="Left button"> </div>
</div>
<div class="col-lg-8">
<div class="form-group mb-1"> <input type="text" class="form-control tags tokenfield" name="variant[value][]" value=""><br>
</div>
</div>
</div>
</div>
解决方案
您正在克隆整个.attribute
div ...其中包括一个input
在...
正如 Barmar 所说.clone(true)
的将“深度克隆”,这意味着它还将克隆属性和事件......但是您正在谈论一个子插件的实例。这就是故事结局不好的地方;)
幸运的是,有办法。您只需在该 new 上创建另一个tokenkenfieldinput
实例。
像这样的东西:
$('.clone').on('click',function(){
var newLine = $(".attribute:first").clone();
$("#variants").append(newLine);
$("#variants").find(".tokenfield").last().tokenfield(); // New instance here.
});
免责声明:未经测试(但是,希望你明白了这个概念......;))
推荐阅读
- ruby-on-rails - 未知属性错误:'availabilities_attributes'
- azure - Azure Data Factory For-Each Activity 迭代失败,但管道成功?
- swift - @AppStorage 可以在 SwiftUI 的环境中使用吗?
- r - R ggplot geom_line() 没有给出想要的结果
- android - 如何在未来的特定时间创建通知?(安卓)
- kotlin - Kotlin Annotation Reflection 将参数注入具有自定义注释的函数
- ms-access - 我如何以编程方式显示启动时隐藏的项目?
- javascript - 如何防止动态创建的表溢出其 div?
- count - 将一个变量的分组总和添加到原始数据中
- c - 如何处理 C 中的 typedef 数据类型(一般来说)?