3

我有时在我的 html 页面中遇到问题,其中 bootstrap-select 中的所有选项都显示在下拉框之外。(附上截图)下拉

在此处输入图像描述

有时仅观察到此问题。这些值是从 api 调用中填充的。这是我的引导选择 css 和 js 参考: https : //cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css,https://cdnjs.cloudflare .com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.js

下面是我的代码片段。

$.ajax({
		url: serverName + "api/v1/get/countrylist",
		type: 'GET',
		headers: {
				"Content-Type": "application/json",
				"Authorization": "Basic " + basicKey,
				"x-access-token": xAccessToken

		},
		success: function(response){
			 // console.log("visa-response: "+ response);
				var countryArr = [];
		response.forEach(function(item){
				countryArr.push({"country": item.country, "fee": item.fee});
			 // console.log("my country " + item.country)
		})
		//console.log("countryArr-response: "+ countryArr[1].country);
		countryArr.forEach(function(item){
				$('.select-country .country-list').append('<option class="'+item.fee + '"' +'data-tokens="'+ item.country+'">' + item.country +'</option>')
		})
	
		$('.selectpicker').selectpicker();
		},
		error: function (exception) {
				console.log(exception);
		}
});

  <div class="col-md-6 select-country">



                            <div class="customer-country text-center" >
                                <label style="margin-right:10px;"><strong>Select Your Country:</strong></label>
                                <select class="selectpicker country-list"  data-live-search="true" data-width="auto" data-style="btn-success" data-dropup-auto="false" standard title="Select Country...">

                                </select>

                            </div>

4

2 回答 2

1

因此,当您使用 selectpicker 类时,它将使用具有SAME CLASS的选择元素创建按钮元素兄弟姐妹。因此,当您仅针对类时,它将同时更新它们。在附加部分使用选择器:$(select.country-list)。它只会更新选择元素。这个对我有用。

$.ajax({
        url: serverName + "api/v1/get/countrylist",
        type: 'GET',
        headers: {
                "Content-Type": "application/json",
                "Authorization": "Basic " + basicKey,
                "x-access-token": xAccessToken

        },
        success: function(response){
             // console.log("visa-response: "+ response);
                var countryArr = [];
        response.forEach(function(item){
                countryArr.push({"country": item.country, "fee": item.fee});
             // console.log("my country " + item.country)
        })
        //console.log("countryArr-response: "+ countryArr[1].country);
        countryArr.forEach(function(item){
                $('select.country-list').append('<option class="'+item.fee + '"' +'data-tokens="'+ item.country+'">' + item.country +'</option>')
        })
    
        $('.selectpicker').selectpicker();
        },
        error: function (exception) {
                console.log(exception);
        }
});

于 2020-08-26T07:53:25.233 回答
1

似乎在动态加载选项时必须重新渲染和刷新 bootstrap-select

$('.selectpicker').selectpicker('render');
$('.selectpicker').selectpicker('refresh');

尝试在成功函数回调的最后一行添加这 2 行

于 2017-11-05T17:26:46.593 回答