[英]jQuery Select2 Plugin
I have a form that allows a user to enter information. 我有一个允许用户输入信息的表格。 It uses the select2 plugin .
它使用select2插件 。
On load, it is initiated to the visible form and works fine. 加载时,它会启动为可见形式,并且可以正常工作。
However, once I add a new div and run it again on all of the classes (original and added) it loses the values that were in the ones prior to adding a new one. 但是,一旦我添加了一个新的div并在所有类(原始的和添加的)上再次运行它,它就会丢失添加新的div之前的值。
Is there a way to preserve this information when the plugin is run? 插件运行时是否可以保留此信息?
When you click on Add Vehicle, it inserts a new div and then runs the plugin again on the .carpool
selector which is in each one of the divs. 当您单击添加载具时,它将插入一个新的div,然后在每个div中的
.carpool
选择器上再次运行该插件。
$(".carpool").select2({
multiple: true,
allowClear: true,
placeHolder: 'Agent Last name',
minimumInputLength: 3,
ajax: {
url: "jsonUser.php",
dataType: 'json',
allowClear: true,
data: function (term) {
return {
term: term, // search term
};
},
results: function (data) { // parse the results into the format expected by Select2.
return {results: data};
}
},
});
Just run the plugin on the div
you're adding. 只需在要添加的
div
上运行插件即可。 You have access to that specific div
, because it's the one you're appending to the DOM. 您可以访问该特定的
div
,因为它是您要附加到DOM的那个。 So if you have something like: 因此,如果您有类似以下内容:
newDiv = $("<div>new stuff</div");
divContainer.append(newDiv);
newDiv.select2({...});
Clearly, you need to alter the above to fit the code you've got. 显然,您需要更改上面的内容以适合您所拥有的代码。 If you need to run the plugin on some child elements of the new
div
, then you'd use something like: 如果您需要在新
div
某些子元素上运行插件,则可以使用以下方法:
newDiv.find("relevant child selector").select2({...});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.