作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用引导 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/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!