简体   繁体   English

使jquery选择的插件与动态添加的下拉列表一起使用

[英]make jquery chosen plugin work with dynamically added dropdowns

Ok so I have an app that adds a new dropdown element to the dom on click, but because the new dropdown is added after chosen is instantiated $('.dropdowns').chosen() it's not turned into a chosen dropdown. 好的,所以我有一个应用程序,可以在单击时向dom添加一个新的下拉菜单元素,但是因为新的下拉菜单是在实例化$('.dropdowns').chosen()之后选择添加的,所以不会变成所选的下拉菜单。

Is there a better way to make dynamically created dropdowns work without having to re-instantiate jquery version of chosen (ie without having to re-call $('.dropdowns').chosen() )? 有没有更好的方法来使动态创建的下拉列表起作用,而不必重新实例化选定的jquery版本(即,不必重新调用$('.dropdowns').chosen() )?

you can simply recall chosen plugin after inserting the dynamic dropdown 您可以在插入动态下拉菜单后简单地调用选定的插件

/*

 code of creating dynamic dropdown

after it add:
*/

$('#ID_OF_DYNAMIC_DROPDOWN').chosen();  

in my case i use: 在我的情况下,我使用:

var i=$('#table_invoice tbody tr').length;
$(".addmore").on('click',function(){
    html = '<tr>';

    html += '<td><select name="item_id[]" id="itemName_'+i+'" class="form-control searchable chosen-rtl">'+ItemOptions+'</select></td>';

    html += '</tr>';

    $('#table_invoice').append(html);
    $('#itemName_'+i).chosen(); 
    i++;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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