Home >Web Front-end >Bootstrap Tutorial >In-depth analysis of drop-down list selection in Bootstrap
This article will give you a detailed introduction to the drop-down list selection in Bootstrap, which is suitable for beginners to learn. I hope it will be helpful to everyone!
Foreword: I have been developing Android for many years and started learning web front-end from scratch. I also found that many blogs are basically copied and reproduced, and they are not clear. So I focused on writing down the things that I feel are unclear on the current blog. After learning the Vue framework, I started to learn native official website development. However, when I learned about Bootstrap's selection, I felt that the online information was confusing, which was very confusing for beginners. Hence this article. [Related recommendations: "bootstrap Tutorial"]
Of course we have to introduce Bootstrap and jQuery
<script type="text/javascript" src="./js/jquery-3.6.0.js"></script> <script type="text/javascript" src="./js/bootstrap.min.js"></script> <link rel="stylesheet" href="./css/bootstrap.min.css">
Directly upload the gif rendering
<select id="selectLeo" class="form-control form-control-placeholder"> <option value="-1" disabled selected hidden>请选择</option> <option value="0" style="color: black;">蕾丝</option> <option value="1" style="color: black;">黑丝</option> <option value="2" style="color: black;">肉丝</option> <option value="3" style="color: black;">杜蕾斯</option> <option value="4" style="color: black;">青椒肉丝</option> </select>
<option value="-1" disabled selected hidden>请选择</option>
.form-control-placeholder{ color: #ccc; }
style="color: black;"
$("#selectLeo").on('change', function () { if ($(this).val() != -1) { //这里是默认的 $('#selectLeo').addClass('black_color').removeClass('gray_color') } })
$('#submit_single_select').click(function () { var options = $('#selectLeo option:selected') $('#singleValue').html('当前选中的value: '+options.val()) $('#singleText').html('当前选中的text: '+options.text()) console.log(options.val()) console.log(options.text()) })
$('#submit_single_repet').click(function () { var options = $('#selectLeo option') options[0].selected = true $('#selectLeo').addClass('gray_color').removeClass('black_color') })
Move the mouse up, the default is white font and light blue background. When I first started learning, I found a lot of information, but most of it was nonsense, so if there are experts here who have concisely modified the css style, you can tell me in the comment area. I have a solution here, which is to use the input drop-down menu to implement this drop-down list function. In that case, the hover can be changed however you want.
Okay, the one-way drop-down list selection is over. You don't understand.
Similarly, first upload a gif rendering
When using this multi-select drop-down list, we also need to reference bootstrap-select. For me, a beginner, I find it a little strange why the official website quotes the full package of bootstrap and does not include this select. The select github address is: bootstrap-select, quoted as follows
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css"> <script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script>
<select multiple="multiple" id="selectLeo_more" class="selectpicker form-control" title="请选择"> <option value="0">蕾丝</option> <option value="1">黑丝</option> <option value="2">肉丝</option> <option value="3">杜蕾斯</option> <option value="4">青椒肉丝</option> </select>
.filter-option-inner-inner{ color: #ccc; }
.dropdown-menu>li>a { display: block; padding: 3px 20px; clear: both; font-weight: 400; line-height: 1.42857143; color: black; white-space: nowrap; }
.dropdown-menu>li>a:hover { display: block; padding: 3px 20px; clear: both; font-weight: 400; line-height: 1.42857143; color: white; white-space: nowrap; background-color: rgba(75, 62, 255, 0.767); }
Okay, this completes the style
$('#selectLeo_more').on('change', function () { if ($(this).val().length != 0) { //这里是默认的 $('.filter-option-inner-inner').css("color", "black") } else { $('.filter-option-inner-inner').css("color", "#ccc") } })
$('#submit_mult_select').click(function () { $('#multValue').html('当前选中的value: '+$('#selectLeo_more').val()) $('#multText').html('当前选中的text: '+$('[data-id|=selectLeo_more').text()) console.log($('#selectLeo_more').val()) })
$('#submit_mult_repet').click(function () { $('#selectLeo_more').selectpicker('deselectAll'); })
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of In-depth analysis of drop-down list selection in Bootstrap. For more information, please follow other related articles on the PHP Chinese website!