【发布时间】:2020-11-25 06:59:27
【问题描述】:
我需要帮助。 我试图动态使用多选下拉列表(从数据库获取记录)。 我从网上得到了多选的代码。下面是代码(它工作得很好)
$('.multi').multi_select({
selectColor: 'white',
selectSize: 'small',
selectText: 'Select Status',
duration: 300,
easing: 'slide',
listMaxHeight: 300,
selectedCount: 3,
sortByText: true,
fillButton: true,
data: {
"BD": "Bangladesh",
"BE": "Belgium",
"BF": "Burkina Faso",
"BG": "Bulgaria",
"BA": "Bosnia and Herzegovina",
"BB": "Barbados",
"WF": "Wallis and Futuna",
"BL": "Saint Barthelemy",
"BM": "Bermuda",
},
buttonWidth:"180px",
onSelect: function(values) {
console.log('return values: ', values);
}
});
$('#get_values').on('click', function(event) {
console.log($('#multi').multi_select('getSelectedValues'));
$('.data-display').remove();
var json = { items: $('#multi').multi_select('getSelectedValues') };
if (json.items.length) {
var ul = $('<ul>', { 'class': 'data-display' }).appendTo('body');
$(json.items).each(function(index, item) {
ul.append(
'<li style="display: block;color:#000000;">' + item + '</li>'
);
});
}
})
$('#clear_values').on('click', function(event) {
$('#multi').multi_select('clearValues');
$('.data-display').slideUp(300, function() {
$(this).remove()
})
})
我想用数据库中的数据替换静态数据。 我想采用的方法是具有加载数据的功能。这是因为这个特定的 DropDown 依赖于另一个表单字段。
那么我可以让函数返回值到上面脚本的数据部分吗?
或者还有其他方法吗?
编辑: 我想添加动态值,例如来自另一个函数的 ajax 响应。
function AnotherFunction(){
ajax_respond (arrayData);
// format ("dataid1" : "datavalue1","data2":"datavalue2", ... and so on dynamically )
return ajax_resond
}
var dbData = AnotherFunction();
data: {dbData },
这可以实现吗?如果可能的话,我如何从 ajax 响应中返回该格式的数组并插入到这个多选的数据 {} 中
【问题讨论】:
-
也许我应该问,我的 jquery 函数通过 json_encode 以这种格式返回数据
0: {id: "8", text: "Granted"} 1: {id: "40", text: "New Filing"} 2: {id: "41", text: "Pending"} 3: {id: "42", text: "Registered"} 4: {id: "43", text: "Inactive"}我如何将这些数据插入到var dbData = AnotherFunction(); data: { here }, //<-------- into this line -
我在这里找到了我一直在寻找的东西another post at stackoverflow
标签: javascript php jquery ajax function