search
HomeWeb Front-endBootstrap TutorialIn-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
bootstrapselectdrop-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">

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>
  • 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>
  • The color value of placeholder is relatively light, so we add a css to it, form-control-placeholder
.form-control-placeholder{
    color: #ccc;
}
  • 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;"

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;)
        }
    })
  • 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())
    })
  • 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;)
    })

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>

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>
  • 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;
}
  • 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;
}
  • 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);
}

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")
        }
    })
  • 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())
    })
  • When you click reset, clear the input box
    $(&#39;#submit_mult_repet&#39;).click(function () {
        $(&#39;#selectLeo_more&#39;).selectpicker(&#39;deselectAll&#39;);
    })

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
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
What is Bootstrap? An Introduction for BeginnersWhat is Bootstrap? An Introduction for BeginnersApr 22, 2025 am 12:07 AM

BootstrapisafreeCSSframeworkthatsimplifieswebdevelopmentbyprovidingpre-styledcomponentsandJavaScriptplugins.It'sidealforcreatingresponsive,mobile-firstwebsites,offeringaflexiblegridsystemforlayoutsandasupportivecommunityforlearningandcustomization.

Bootstrap Demystified: A Simple ExplanationBootstrap Demystified: A Simple ExplanationApr 21, 2025 am 12:13 AM

Bootstrapisafree,open-sourceCSSframeworkthathelpscreateresponsive,mobile-firstwebsites.1)Itoffersagridsystemforlayoutflexibility,2)includespre-styledcomponentsforquickdesign,and3)ishighlycustomizabletoavoidgenericlooks,butrequiresunderstandingCSStoop

Bootstrap vs. React: Choosing the Right ApproachBootstrap vs. React: Choosing the Right ApproachApr 20, 2025 am 12:09 AM

Bootstrap is suitable for quick construction and small projects, while React is suitable for complex and interactive applications. 1) Bootstrap provides predefined CSS and JavaScript components to simplify responsive interface development. 2) React improves performance and interactivity through component development and virtual DOM.

Bootstrap's Purpose: Building Consistent and Attractive WebsitesBootstrap's Purpose: Building Consistent and Attractive WebsitesApr 19, 2025 am 12:07 AM

The main purpose of Bootstrap is to help developers quickly build responsive, mobile-first websites. Its core functions include: 1. Responsive design, which realizes layout adjustments of different devices through a grid system; 2. Predefined components, such as navigation bars and modal boxes, ensure aesthetics and cross-browser compatibility; 3. Support customization and extensions, and use Sass variables and mixins to adjust styles.

Bootstrap vs. Other Frameworks: A Comparative OverviewBootstrap vs. Other Frameworks: A Comparative OverviewApr 18, 2025 am 12:06 AM

Bootstrap is better than TailwindCSS, Foundation, and Bulma because it is easy to use and quickly develop responsive websites. 1.Bootstrap provides a rich library of predefined styles and components. 2. Its CSS and JavaScript libraries support responsive design and interactive functions. 3. Suitable for rapid development, but custom styles may be more complicated.

Integrating Bootstrap Styles in React: Methods and TechniquesIntegrating Bootstrap Styles in React: Methods and TechniquesApr 17, 2025 am 12:04 AM

Integrating Bootstrap in React projects can be done in two ways: 1) introduced using CDN, suitable for small projects or rapid prototyping; 2) installation using npm package manager, suitable for scenarios that require deep customization. With these methods, you can quickly build beautiful and responsive user interfaces in React.

Bootstrap in React: Advantages and Best PracticesBootstrap in React: Advantages and Best PracticesApr 16, 2025 am 12:17 AM

Advantages of integrating Bootstrap into React projects include: 1) rapid development, 2) consistency and maintainability, and 3) responsive design. By directly introducing CSS files or using the React-Bootstrap library, you can use Bootstrap's components and styles efficiently in your React project.

Bootstrap: A Quick Guide to Web FrameworksBootstrap: A Quick Guide to Web FrameworksApr 15, 2025 am 12:10 AM

Bootstrap is a framework developed by Twitter to help quickly build responsive, mobile-first websites and applications. 1. Ease of use and rich component libraries make development faster. 2. The huge community provides support and solutions. 3. Introduce and use class names to control styles through CDN, such as creating responsive grids. 4. Customizable styles and extension components. 5. Advantages include rapid development and responsive design, while disadvantages are style consistency and learning curve.

See all articles

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor