So I am trying to append a select, and it works fine when the class of this plugin, bootstrap-select, is not attached:
所以我试图附加一个选择,当这个插件的类bootstrap-select没有附加时,它工作正常:
var select = $("<select class='new-select'></select>");
$('#selects-container').append(select);
However, when I add the class 'selectpicker', the select is appended, but it's not processed through the javascript of the plugin, and is therefore not visible.
但是,当我添加类'selectpicker'时,会附加select,但它不会通过插件的javascript处理,因此不可见。
var select = $("<select class='new-select selectpicker'></select>");
$('#selects-container').append(select);
So my question is how to get the new appended select to be ran through the javascript of the plugin and be formatted as would a select that was present on screen load.
所以我的问题是如何让新的附加选择通过插件的javascript运行,并被格式化为屏幕加载时出现的选择。
Sincere thanks for the help, it is greatly appreciated.
真诚地感谢您的帮助,非常感谢。
1 个解决方案
#1
1
So there are a number of ways to declare new DOM nodes using jQuery. For your task specifically, you'll want to specify an options object as the second argument, then add attributes to that, rather than in the selection string itself.
因此,有许多方法可以使用jQuery声明新的DOM节点。具体来说,您需要将选项对象指定为第二个参数,然后将属性添加到该对象,而不是选择字符串本身。
Try this:
var select = $('<select></select>', {
'class': 'new-select selectpicker'
});
$('#selects-container').append(select);
Also, be aware of this, from the docs:
另外,请从文档中了解这一点:
The name "class" must be quoted in the object since it is a JavaScript reserved word, and "className" cannot be used since it refers to the DOM property, not the attribute.
必须在对象中引用名称“class”,因为它是一个JavaScript保留字,并且不能使用“className”,因为它引用的是DOM属性,而不是属性。
#1
1
So there are a number of ways to declare new DOM nodes using jQuery. For your task specifically, you'll want to specify an options object as the second argument, then add attributes to that, rather than in the selection string itself.
因此,有许多方法可以使用jQuery声明新的DOM节点。具体来说,您需要将选项对象指定为第二个参数,然后将属性添加到该对象,而不是选择字符串本身。
Try this:
var select = $('<select></select>', {
'class': 'new-select selectpicker'
});
$('#selects-container').append(select);
Also, be aware of this, from the docs:
另外,请从文档中了解这一点:
The name "class" must be quoted in the object since it is a JavaScript reserved word, and "className" cannot be used since it refers to the DOM property, not the attribute.
必须在对象中引用名称“class”,因为它是一个JavaScript保留字,并且不能使用“className”,因为它引用的是DOM属性,而不是属性。