动态创建的元素上的 Bootstrap 标签输入
我使用 Bootstrap Tags 输入(带有预先输入),它在我的静态输入上效果很好。 但我也创建了动态输入字段 - 这些是行不通的。
如何将动态创建的元素附加到tagsinput?
主要代码:
var depends = ["jquery"];
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches;
matches = [];
substrRegex = new RegExp(q, 'i');
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var field = $('.resource_depend');
field.tagsinput({
confirmKeys: [13, 44],
itemValue: 'id',
typeaheadjs:({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'depends',
source: substringMatcher(depends)
}),
maxTags: 20,
maxChars: 50
});
后来我有(类似的东西):
...
var cnt = 0;
var addFragment = myStaticFragment.cloneNode(true);
addFragment.id = 'dynamic_'+ cnt
myStaticFragment.parentNode.appendChild(addFragment);
cnt += 1;
注意:我有几个使用独立标签的独立字段。
使用开发人员工具,似乎添加了一些代码,但不是像静态输入那样的功能。
更新:我尝试从“myStaticFragment”中删除“data-role”属性,并在每次添加时调用主代码。它只是将输入字段加倍,没有任何区别。
I use Bootstrap Tags Input (with typeahead) and it works great on my static inputs.
But I also create dynamic input fields - these won't work.
How can I attach a dynamically created element to tagsinput?
Main code:
var depends = ["jquery"];
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches;
matches = [];
substrRegex = new RegExp(q, 'i');
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var field = $('.resource_depend');
field.tagsinput({
confirmKeys: [13, 44],
itemValue: 'id',
typeaheadjs:({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'depends',
source: substringMatcher(depends)
}),
maxTags: 20,
maxChars: 50
});
Later I have (something like):
...
var cnt = 0;
var addFragment = myStaticFragment.cloneNode(true);
addFragment.id = 'dynamic_'+ cnt
myStaticFragment.parentNode.appendChild(addFragment);
cnt += 1;
Note: I have several independent fields that use independent tags.
With the developer tools, it seems to add some code, but not the functionality like on the static inputs.
UPDATE: I tried to remove the "data-role" attribute from 'myStaticFragment' and call the main code upon each addition. It just doubled the input field without any difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前没有使用过 typeahead 库,所以这是基于我对类似 javascript UI 库的经验。如果它类似于 KendoUI 或 jQueryUI 库,那么您需要在元素位于 DOM 上之后添加功能。
因此,既然您在 javascipt 中进一步使用了 jQuery,我将利用它。
通过更改此块:
到(例如):
从理论上讲,至少应该让您沿着正确的轨道走得足够远,以便能够弄清楚这个特定库所需的确切方法。
更新:考虑到旧版本的 jQuery 没有
.data()
函数,一种选择可能是将元素的设置存储在变量中。类似以下内容:可能有用吗?
I haven't used the typeahead library before, so this based on my experience with similar javascript UI libraries. If it's anything like the KendoUI or jQueryUI libraries, then you need to add the functionality AFTER the element is on the DOM.
So, since you use bits of jQuery further up the javascipt, I'll utilise that.
By changing this block:
To (for example):
Should, theoretically, at least point you far enough down the right tracks to be able to suss out the exact methodology that this particular library requires.
UPDATE: to account for older versions of jQuery that don't have the
.data()
function, one option may be to store the settings for the element in a variable. Something along the lines of:Might be of use?