如何将类应用于jQueryajax调用的结果


How to apply a class to the result of a jQuery ajax call?

我试图找到这个问题的答案,但没有成功。

我有以下代码:

jQuery("#categories_parent_id").change(function() {
            id = jQuery("#categories_parent_id").val();
            jQuery.ajax({
                type: "GET",
                url: 'index.php?option=com_jomdirectory&task=getselect&format=raw',
                dataType: "html",
                data: "id=" + id,
                success: function(data) {
                    jQuery('#selectlist').html(data);
                }
            });
        });

ajax结果选择应该有类"form control"answers"selected select",我尝试在ajax视图中添加它们,但它没有样式。

在ajax结果之后,我该如何将有问题的类添加到特定的select中?

感谢

J

尝试使用.find()查找html数据中的元素,然后使用addClass()查找

jQuery('#selectlist').html(data).find('select').addClass('form-control chosen-select');

稍后编辑,因为@A.Wolff提到您需要在新添加的select元素上调用插件。

success: function(data) {
    jQuery('#selectlist').html(data).find('select').chosen();
}

使用.addClass()

success: function(data) {
    jQuery('#selectlist').html(data).addClass("form-control chosen-select");
}