Table of Contents
Prerequisite
1. Basic single-item selection drop-down list
1.1 HTML code
1.2 js code to monitor and obtain the value
1.3 How to modify the drop-down list: hover
2. Multiple selection, drop-down list
2.1 The code on html
2.2 Multi-select select monitoring and obtaining values
Home Web Front-end Bootstrap Tutorial In-depth analysis of drop-down list selection in Bootstrap

In-depth analysis of drop-down list selection in Bootstrap

Oct 29, 2021 am 11:33 AM
bootstrap select drop-down list

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!

In-depth analysis of drop-down list selection in Bootstrap

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"]

Prerequisite

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">
Copy after login

1. Basic single-item selection drop-down list

Directly upload the gif rendering

In-depth analysis of drop-down list selection in Bootstrap

1.1 HTML code

        <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>
Copy after login
  • form-control is the css style that comes with bootstrap
  • We will find that it lacks a placeholder, we can add a placeholder to it in the following way
<option value="-1" disabled selected hidden>请选择</option>
Copy after login
  • The color value of placeholder is relatively light, so we add a css to it, form-control-placeholder
.form-control-placeholder{
    color: #ccc;
}
Copy after login
  • After adding it, you will find the color in the drop-down list The value also changes accordingly. Then we can add our own color value to the option and it will not change
style="color: black;"
Copy after login

1.2 js code to monitor and obtain the value

  • When we select the value, the box It will turn black inside. If you click Reset, it will turn back to gray. At this time, make a monitor for the input box. If value==-1, it will be gray. This listener will not be triggered when you click reset, so I put it in the reset method when it turns gray. black_color and gray_color are two css styles, in which there are only color values
    $("#selectLeo").on(&#39;change&#39;, function () {
        if ($(this).val() != -1) {
            //这里是默认的
            $(&#39;#selectLeo&#39;).addClass(&#39;black_color&#39;).removeClass(&#39;gray_color&#39;)
        }
    })
Copy after login
  • When you click the submit button, get the currently selected value and text value, singleValue and singleText are the two I placed Display text
    $(&#39;#submit_single_select&#39;).click(function () {
        var options = $(&#39;#selectLeo option:selected&#39;)
        $(&#39;#singleValue&#39;).html(&#39;当前选中的value: &#39;+options.val())
        $(&#39;#singleText&#39;).html(&#39;当前选中的text: &#39;+options.text())
        console.log(options.val())
        console.log(options.text())
    })
Copy after login
  • When we click reset, we have to return to the placeholde and the color changes back to gray
    $(&#39;#submit_single_repet&#39;).click(function () {
        var options = $(&#39;#selectLeo option&#39;)
        options[0].selected = true
        $(&#39;#selectLeo&#39;).addClass(&#39;gray_color&#39;).removeClass(&#39;black_color&#39;)
    })
Copy after login

1.3 How to modify the drop-down list: hover

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.

2. Multiple selection, drop-down list

Similarly, first upload a gif rendering

In-depth analysis of drop-down list selection in Bootstrap

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>
Copy after login

2.1 The code on html

        <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>
Copy after login
  • passes multiple="multiple "Set to multi-select; class="selectpicker form-control" is bootstrap's own CSS style; title="Please select" is our placeholder
  • Change the color value of the placeholder through the following css style
.filter-option-inner-inner{
    color: #ccc;
}
Copy after login
  • Change the font color of the drop-down list through the css style below
.dropdown-menu>li>a {
    display: block;
    padding: 3px 20px;
    clear: both;
    font-weight: 400;
    line-height: 1.42857143;
    color: black;
    white-space: nowrap;
}
Copy after login
  • Change the font and background color display after the mouse moves up
.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);
}
Copy after login

Okay, this completes the style

2.2 Multi-select select monitoring and obtaining values

  • Multi-select drop-down list Monitoring, when the monitoring here has a selected value, the font color changes to black, and when there is no value, it changes to gray. This is a little different from the radio selection. When reset, this monitoring is effective
    $(&#39;#selectLeo_more&#39;).on(&#39;change&#39;, function () {
        if ($(this).val().length != 0) {
            //这里是默认的
            $(&#39;.filter-option-inner-inner&#39;).css("color", "black")
        } else {
            $(&#39;.filter-option-inner-inner&#39;).css("color", "#ccc")
        }
    })
Copy after login
  • Click submit to get the selected value
    $(&#39;#submit_mult_select&#39;).click(function () {
        $(&#39;#multValue&#39;).html(&#39;当前选中的value: &#39;+$(&#39;#selectLeo_more&#39;).val())
        $(&#39;#multText&#39;).html(&#39;当前选中的text: &#39;+$(&#39;[data-id|=selectLeo_more&#39;).text())
        console.log($(&#39;#selectLeo_more&#39;).val())
    })
Copy after login
  • When you click reset, clear the input box
    $(&#39;#submit_mult_repet&#39;).click(function () {
        $(&#39;#selectLeo_more&#39;).selectpicker(&#39;deselectAll&#39;);
    })
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to get the bootstrap search bar How to get the bootstrap search bar Apr 07, 2025 pm 03:33 PM

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles