gpt4 book ai didi

javascript - 我使用 Bootstrap 多选的下拉列表未填充

转载 作者:行者123 更新时间:2023-12-03 00:04:23 25 4
gpt4 key购买 nike

我正在尝试使用引导 javascript 和 css 文件创建多选下拉列表。不幸的是,我动态创建的选项集(使用 AJAX)似乎没有填充列表。

我知道它一定与多选功能有关,因为当我将其设置为单选下拉菜单时,它效果很好。

不知道是否与这个函数的调用时间有关。该列表不会在文档准备好时调用,而是在 ajax 调用成功之后调用。

这是代码:

function Participants(sid){
console.log(sid);
$('#participants').empty()
$('#participants').append("<option>Loading......</option>");
$.ajax({
method:"POST",
url:"participants_dropdown.php?sid="+sid,
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(data){
console.log('success')
$('#participants').empty();
$('#participants').append("<option value='0'>---Select Participants---</option>");
$.each(data, function(i, item){
$('#participants').append('<option value="'+data[i].facility_id+'">Call-Sign: '+data[i].call_sign+' - Operator: '+data[i].operator+' - Primary Programming: '+data[i].primary_programming+'</option>');
});
},
complete: function(){
}
});
$('#participants').multiselect('destroy');
$('#participants').multiselect();
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.js"></script>





<div class="col-md-4">
<h3><span>2) Select Market Participants</span></h3>
<select id="participants" multiple class="form-control">
</select>
</div>


<!---sample JSON
[{"operator":"Nexstar Media Group Inc","owner":"Mission Broadcasting Inc","call_sign":"KRBC-TV","facility_id":"306","primary_programming":"NBC"},{"operator":"Nexstar Media Group Inc","owner":"Nexstar Media Group Inc","call_sign":"KTAB-TV","facility_id":"59988","primary_programming":"CBS"},{"operator":"Prime Time Christian Broadcasting Incorporated","owner":"Prime Time Christian Broadcasting Incorporated","call_sign":"KPCB-DT","facility_id":"77452","primary_programming":"REL"},{"operator":"Sinclair Broadcast Group Inc","owner":"Sinclair Broadcast Group Inc","call_sign":"KTXS-TV","facility_id":"308","primary_programming":"ABC"},{"operator":"TEGNA Inc","owner":"TEGNA Inc","call_sign":"KXVA","facility_id":"62293","primary_programming":"FOX"}]--->

任何指导将不胜感激。我尝试过类似帖子中建议的各种解决方案,但没有成功。

最佳答案

在您的 Participant 函数中,您有一个 ajax 调用。因为这是一个异步调用,所以您需要在成功片段内移动销毁并创建多选:

function Participants(sid){
console.log(sid);
$('#participants').empty()
$('#participants').append("<option>Loading......</option>");
$.ajax({
method:"POST",
url:"participants_dropdown.php?sid="+sid,
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(data){
console.log('success')
$('#participants').multiselect('destroy');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ HERE
$('#participants').empty();
$('#participants').append("<option value='0'>---Select Participants---</option>");
$.each(data, function(i, item){
$('#participants').append('<option value="'+data[i].facility_id+'">Call-Sign: '+data[i].call_sign+' - Operator: '+data[i].operator+' - Primary Programming: '+data[i].primary_programming+'</option>');
});
$('#participants').multiselect();
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ HERE
},
complete: function(){
}
});
}

代码片段(按钮模拟ajax调用):

$('#participants').empty()
$('#participants').append("<option>Loading......</option>");
$('#participants').multiselect();


$('#btn').on('click', function(e) {
var data = [{"operator":"Nexstar Media Group Inc","owner":"Mission Broadcasting Inc","call_sign":"KRBC-TV","facility_id":"306","primary_programming":"NBC"},{"operator":"Nexstar Media Group Inc","owner":"Nexstar Media Group Inc","call_sign":"KTAB-TV","facility_id":"59988","primary_programming":"CBS"},{"operator":"Prime Time Christian Broadcasting Incorporated","owner":"Prime Time Christian Broadcasting Incorporated","call_sign":"KPCB-DT","facility_id":"77452","primary_programming":"REL"},{"operator":"Sinclair Broadcast Group Inc","owner":"Sinclair Broadcast Group Inc","call_sign":"KTXS-TV","facility_id":"308","primary_programming":"ABC"},{"operator":"TEGNA Inc","owner":"TEGNA Inc","call_sign":"KXVA","facility_id":"62293","primary_programming":"FOX"}];
$('#participants').multiselect('destroy');
$('#participants').empty();
$('#participants').append("<option value='0'>---Select Participants---</option>");
$.each(data, function(i, item){
$('#participants').append('<option value="'+data[i].facility_id+'">Call-Sign: '+data[i].call_sign+' - Operator: '+data[i].operator+' - Primary Programming: '+data[i].primary_programming+'</option>');
});
$('#participants').multiselect();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.js"></script>

<div class="col-md-4">
<h3><span>2) Select Market Participants</span></h3>
<select id="participants" multiple class="form-control">
</select>
<button id="btn" class="btn btn-primary">Click Me</button>
</div>

关于javascript - 我使用 Bootstrap 多选的下拉列表未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55032306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com