Bootstrap select destroy removes the original select from DOM

[亡魂溺海] 提交于 2019-12-08 20:33:16

问题


I am currently using the Bootstrap select plugin in one of my projects. I have a dynamically generated table that has select elements which I then convert to bootstrap select using the command:

_tr.find('select.selectpicker').selectpicker({ size: 10 });

Where _tr is the row where I added the select elements. This is working fine, the bootstrap select is shown and in firebug I still see the original select element.

Now every row has a button where it removes ( destroy ) the bootstrap select. But it removes also the original select, and thats the poblem because I still need the original select to do something else.

I use the following command to destroy:

 $('select#begin' + _week + _day).selectpicker("destroy");
 $('select#end' + _week + _day).selectpicker("destroy");
 $('select#pause' + _week + _day).selectpicker("destroy");

In other plugins if you use destroy, it shows the original element again. Is this an issue in the boostrap select plugin or is there another way to remove the bootstrap select and reshow the original select.


回答1:


Found the solution, I edit the plugin function destroy where instead of removing the original element, I show it back:

remove: function () {
  this.$newElement.remove();
  this.$element.show(); // First it was this.$element.remove();
}



回答2:


I am really appreciate your method because it helps me a lot. But I found a way to improve your method without changing the original code. Here is what I did:

jQuery.fn.selectpicker.Constructor.prototype.removeDiv = function () {
    this.$newElement.remove();
    this.$element.show();
};

After that, we can use:

jQuery("#some_id").selectpicker("removeDiv");


来源:https://stackoverflow.com/questions/27333816/bootstrap-select-destroy-removes-the-original-select-from-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!