我正在使用的jQuery插件选择(由收获)。它在(文档).ready上运行得很好,但是我有一个按钮,当单击该按钮时,它将使用ajax动态创建更多的选择对象,我希望使用“选择”特性。但是,只有原始的select元素具有“选择”的特性,并且新的(动态创建的)不能工作。我使用jQuery.get来追加新元素。下面是代码的示例:
jQuery(".select").chosen();//this one loads correctly
jQuery("#add-stage").click(function() {
jQuery.get('/myurl',{},function(response) {
//response contains html with 2 more select elements with 'select' class
jQuery('#stages').append(response);
jQuery(".select").chosen();//this one doesn't seem to do anything :-(
});
});我在想,我需要一个.live()函数,但我还没有弄清楚。任何帮助都是非常感谢的!
注释-我并不试图像使用 trigger("liszt:updated");的文档中所指定的那样动态加载新选项。
发布于 2011-12-01 23:42:43
确保response元素具有select类。
console.log( response ); // to verify也许也是一个好主意,只将插件应用于新的元素。
jQuery(".select").chosen();
jQuery("#add-stage").click(function() {
jQuery.get('/myurl',{},function(response) {
console.log( response ); // verify the response
var $response = $(response); // create the elements
$response.filter('.select').chosen(); // apply to top level elems
$response.find('.select').chosen(); // apply to nested elems
$response.appendTo('#stages');
});
});此外,如果/myurl返回整个HTML,则可能会得到不可预测的结果。
发布于 2014-04-07 16:16:35
在您编写(填充选择) .write之后
$(".select").trigger("chosen:updated");发布于 2012-06-11 12:52:56
我的选择也有类似的问题。在用户单击链接后,我试图动态添加一个新的选择。我克隆了前面的select,然后添加了克隆,但是所选的选项无法工作。解决方案是剥离选定的类并添加元素,将克隆放在DOM中,然后再次运行strip:
clonedSelect.find('select').removeClass('chzndone').css({'display':'block'}).removeAttr('id').next('div').remove();
mySelect.after(clonedSelect);
clonedSelect.find('select').chosen();https://stackoverflow.com/questions/8349888
复制相似问题