我想用从数据库中提取的数据填充这个 jQuery 对象。一旦我有了这些数据,我怎样才能让它填充显示已选择内容的字段(本质上为用户提供删除现有条件的选项)
http://harvesthq.github.io/chosen/
这是示例网站,我使用的特定容器称为“多选”
$(".chosen-select").chosen({
no_results_text: "Oops, nothing found!",
width: "95%"
});
最佳答案
$('#chosen-select').chosen();
$.ajax({
type: "POST",
url: '/echo/json/',
data: {
json: JSON.stringify({
options: ['American Black Bear',
'Asiatic Black Bear',
'Brown Bear',
'Giant Panda',
'Sloth Bear',
'Sun Bear',
'Polar Bear',
'Spectacled Bear'],
selected: ['Brown Bear', 'Sun Bear']
}),
delay: 0
}
}).done(function (response) {
var a, element;
for (i = 0; i < response.options.length; i++) {
a = response.selected.indexOf(response.options[i]);
element = $("<option/>").text(response.options[i]);
if (a >= 0) element.attr('selected', 'selected');
element.appendTo($("#chosen-select"));
};
$("#chosen-select").trigger("chosen:updated");
});
您可以像平常一样填充您的选择元素,设置您想要选择的项目,然后触发更新事件(“chosen:updated”)
关于php - 预填充 JQuery 多选工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22204170/