小码哥的IT人生

Bootstrap 5 选择菜单

Bootstrap 2023-08-10 09:26:55小码哥的IT人生shichen

Bootstrap 5 选择菜单

选择菜单

选择菜单(选择一项):

多选菜单(按住 ctrl 或 shift(或用鼠标拖动)可选择多个):

如果您希望允许用户从多个选项中进行选择,请使用选择菜单。

如需在 Bootstrap 5 中设置选择菜单的样式,请将 .form-select 类添加到 <select> 元素:

示例代码:

<select class="form-select">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
</select>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>选择菜单</h2>
  <p>如需在 Bootstrap 5 中设置选择菜单的样式,请将 .form-select 类添加到 select 元素:</p>
  <form action="/demo/demo_form.php">
    <label for="sel1" class="form-label">选择菜单(选择一项):</label>
    <select class="form-select" id="sel1" name="sellist1">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
    </select>
    <br>
    <label for="sel2" class="form-label">多选菜单(按住 ctrl 或 shift(或用鼠标拖动)可选择多个):</label>
    <select multiple class="form-select" id="sel2" name="sellist2">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
    </select>
    <button type="submit" class="btn btn-primary mt-3">提交</button>
  </form>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

选择菜单的大小

请使用 .form-select-lg.form-select-sm 类来改变选择菜单的大小:

示例代码:

<select class="form-select form-select-lg">
<select class="form-select">
<select class="form-select form-select-sm">

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>选择菜单的大小</h2>
  <p>请使用 .form-select-lg 或 .form-select-sm 类来改变选择菜单的大小:</p>
  <form>
   <select class="form-select form-select-lg">
     <option>1</option>
     <option>2</option>
     <option>3</option>
     <option>4</option>
   </select>
   <select class="form-select mt-3">
     <option>1</option>
     <option>2</option>
     <option>3</option>
     <option>4</option>
   </select>
   <select class="form-select form-select-sm mt-3">
     <option>1</option>
     <option>2</option>
     <option>3</option>
     <option>4</option>
   </select>
  </form>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

禁用选择菜单

请使用 disabled 属性禁用选择菜单:

示例代码:

<select class="form-select" disabled>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
</select>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>禁用选择菜单</h2>
  <p>请使用 disabled 属性禁用选择菜单:</p>
  <form>
   <select class="form-select" disabled>
     <option>1</option>
     <option>2</option>
     <option>3</option>
     <option>4</option>
   </select>
  </form>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

数据列表

Bootstrap 还可设置数据列表的样式,它是 <input> 元素的预定义选项列表:

请从列表中选择您的浏览器:

示例代码:

<label for="browser" class="form-label">请从列表中选择您的浏览器:</label>
<input class="form-control" list="browsers" name="browser" id="browser">
<datalist id="browsers">
  <option value="Edge">
  <option value="Firefox">
  <option value="Chrome">
  <option value="Opera">
  <option value="Safari">
</datalist>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>数据列表</h2>
  <form action="/demo/demo_form.php">
    <label for="browser" class="form-label">请从列表中选择您的浏览器:</label>
    <input class="form-control" list="browsers" name="browser" id="browser">
    <datalist id="browsers">
      <option value="Edge">
      <option value="Firefox">
      <option value="Chrome">
      <option value="Opera">
      <option value="Safari">
    </datalist>
    <button type="submit" class="btn btn-primary mt-3">提交</button>
  </form>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024