在BootStrap的modal(模态框)中使用Select2(下拉框带搜索框)

github地址:https://github.com/ttskch/select2-bootstrap4-theme

引入Selelct2的CSS文件,按路径查找

在这里插入图片描述

其次引入Selelct2的JavaScript文件

地址:https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js

使用,这里我就使用几条数据

<select class="single-select">
	<optionvalue="United States">哈哈哈</option>
	<option value="United Kingdom">United Kingdom</option>
	<option value="Afghanistan">Afghanistan</option>
	<option value="Aland Islands">Aland Islands</option>
	<option value="United Kingdom">United Kingdom</option>
	<option value="Afghanistan">Afghanistan</option>
	<option value="Aland Islands">Aland Islands</option>
</select>

在JavaScript中引入

$('.single-select').select2({
	theme: 'bootstrap4',
	// 这个是需要在模态框中使用必须加的语句,搜索框渲染时绑定到这个id的div
  	dropdownParent:$('#course-lesson-form'),
 	width: $(this).data('width') ? $(this).data('width') : $(this).hasClass('w-100') ? '100%' : 'style',
  	placeholder: $(this).data('placeholder'),
 	allowClear: Boolean($(this).data('allow-clear')),
});
dropdownParent:$(’#course-lesson-form’):这个是需要在模态框中使用必须加的语句,因为要让模态框显示出来再去执行下拉框的JavaScript的代码,在使用模态框与Select2结合最坑的地方就是这个了。
效果图

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值