加载动态"选择"选择元素 [英] Loading dynamic "chosen" select elements
问题描述
我使用的jQuery插件选择(嘉实)。这是工作的罚款上(文件)。就绪,但我有一个按钮,点击后,使用Ajax动态地创建,我想用选择功能的更多选择对象。然而,只有原来的选择元件具有选择的特性,和新的(动态创建的)不工作。我使用jQuery.get追加新要素。这里是code的例子:
I am using the jQuery plugin chosen (by Harvest). It is working fine on (document).ready, but I have a button that, when clicked, uses ajax to dynamically create more select objects that I want to use the "chosen" feature. However, only the original select elements have the "chosen" features, and the new (dynamically created) do not work. I am using jQuery.get to append the new elements. Here is a sample of the code:
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()函数的地方,但我一直没能明白这一点呢。任何帮助是非常AP preciated!
I was thinking that I need a .live() function somewhere, but I haven't been able to figure that out yet. Any help is much appreciated!
注意 - 我不是要动态加载新的选项,使用指定的文档中 触发(李斯特:更新);
推荐答案
确保响应
元素具有选择
类。
console.log( response ); // to verify
也可能是一个好主意的插件,仅适用于新元素(S)。
May also be a good idea to only apply the plugin to the new element(s).
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文档,你可能会得到联合国predictable结果。
Also, if /myurl
is returning an entire HTML document, you may get unpredictable results.
这篇关于加载动态"选择"选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!