javascript - 如何在引导多选中获取 MySQL 数据?
问题描述
我为 MySQL 数据库创建了一个管理面板,当我单击编辑按钮时,我想打印 Bootstrap 多选中的所有数据。如果我没有使用 Bootstrap 多选,只是普通的多选,它可以工作。我怎样才能做到这一点?
这是它的样子(在按钮中,它显示当前数据,但是当我打开时,它没有显示在选择菜单中):
索引.php
<select name="classification[]" id="classification" multiple="multiple">
<?php
while($row = $classificationResult -> fetch_array()) {
?>
<option value="<?php echo $row['classification_id'];?>"><?php echo $row['name'];?></option>
<?php
}
?>
</select>
<script>
// If I didn't add the multi select jquery, it's working
$('#classification').multiselect({
nonSelectedText: 'Select Framework',
maxHeight : 400,
includeSelectAllOption : false,
enableFiltering : false,
buttonWidth : '100%',
dropRight : true
});
$(document).on('click', '.edit_data', function(){
$("#classification option").prop("selected", false);
var data_id = $(this).attr("id");
$.ajax({
url:"fetchRole.php",
method:"POST",
data:{'data_id':data_id},
dataType:"json",
success:function(data){
$.each(data.classifications, function(i, e) {
$("#classification option[value='" + e + "']").prop("selected", true);
});
$('#data_id').val(data.id);
$('#insert').val("Edit");
$('#add_data_Modal').modal('show');
}
});
});
</script>
fetchRole.php
$query2 = $conn -> prepare("SELECT classification.classification_id AS class_id
FROM classification
LEFT JOIN role_classification ON classification.classification_id = role_classification.classification_id
WHERE role_classification.role_id = ?");
$query2 -> bind_param('i', $role['id']);
$query2 -> execute();
$result2 = $query2 -> get_result();
$query2 -> close();
while ($classification = $result2 -> fetch_assoc()) {
$classificationIdList[] = $classification["class_id"];
}
$return = array_merge($role, ["classifications" => $classificationIdList]);
echo json_encode($return);
解决方案
使用 设置新的选定选项后,您需要刷新多选.prop("selected", true)
。
$.each(data.classifications, function(i, e) {
$("#classification option[value='" + e + "']").prop("selected", true);
});
$('#classification').multiselect('refresh');
方法说明refresh
:
此方法用于根据 select 中当前选择的选项刷新选中的复选框。
您可以在此处查看可用方法的完整列表。
推荐阅读
- r - 将list的索引添加到bind_rows?
- mysql - 无法在 Windows 中将 MySQL 与 Liferay 连接
- jquery - 退格后jquery自动完成工作
- angular - 在 Angular6 中导入 ngrx 效果
- php - 时差增加 30 分钟
- sql - 我必须根据存储在第二个表中的标志值显示和隐藏表的列
- django - 如何在 django 中对外键模型执行过滤、聚合或注释
- django - 更新 null=True IntegerField 时 Django REST“需要一个有效的整数”
- java - jar文件的Java依赖
- java - Groovy 方法的效率