简体   繁体   中英

Selectpicker on dynamically created select

i'm using bootstrap-select in a form with several selects, but i clone or create new selects with new ids and same class and use:

$(".selectpicker").selectpicker("render"); or
$(".selectpicker").selectpicker("refresh");

and the new selects doesn't get updated, i also try cloning both normal select and created bootstrap-select change ids and data-id and $(".selectpicker").selectpicker("refresh"); but this doesn't work.

How can this be done.

UPDATE

This is the basic HTML, basically i'm using Django formset, here i have a form with a table, each row is a new form instance with inputs and selects.

<tr class="dynamic-form">
 <td class="" style="text-align: center">
  <input type="number" name="caracteristicas-0-ORDER" value="1" id="id_caracteristicas-0-ORDER">
 </td>
 <td class="" style="text-align: center">
  <select name="caracteristicas-0-caract" data-live-search="true" class="form-control selectpicker" id="id_caracteristicas-0-caract" style="display: none;">
  <option value="1" selected="">Nombre</option>
  <option value="2">Ref</option>
  <option value="3">Modelo</option>
 </select>
</td>

The Javascript is basically this:

---- onClickButton ----
row = tr.clone(true); <----- this was the problem
/* clear values, change id's, etc. */
row.insertBefore(lastRow).show();
row.find('.bootstrap-select').remove();
$('.selectpicker').selectpicker("render");

After reading all library code, the problem was in this line:

row = tr.clone(true);

When you use clone(true) you get a copy of the element AND all events handlers etc. You need to get a clean copy of the element structure only.

So the solution is:

row = tr.clone();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM