Làm cách nào để nhận giá trị của thẻ chọn?

Chọn các phần tử thường có hai giá trị mà bạn muốn truy cập. Đầu tiên, có giá trị được gửi đến máy chủ, điều này thật dễ dàng

1

2

$[ "#myselect" ].val[];

// => 1

Thứ hai là giá trị văn bản của lựa chọn. Ví dụ: sử dụng hộp chọn sau

1

2

3

4

5

6

7

Mr

Mrs

Ms

Dr

Prof

Nếu bạn muốn lấy chuỗi "Mr" nếu tùy chọn đầu tiên được chọn [thay vì chỉ "1"] bạn sẽ thực hiện theo cách sau

Xây dựng các yếu tố đầu vào được chọn rất đơn giản khi làm việc trên các dự án web. Nhưng với sự xuất hiện của các khung và thư viện JavaScript, việc xây dựng các yếu tố đầu vào có thể hơi phức tạp vì bạn cần suy nghĩ từ quan điểm dữ liệu và lên kế hoạch trước cho tất cả các yếu tố đầu vào sẽ có trong ứng dụng

Trong React, tất cả các yếu tố đầu vào cần được kiểm soát bởi trạng thái, bao gồm cả yếu tố ____45_______ . Trong hướng dẫn này, bạn sẽ tìm hiểu cách lấy giá trị đã chọn từ một đầu vào được chọn trong React.

Đã ánh xạ Chọn đầu vào

Bắt đầu bằng cách tạo một mảng các đối tượng để sử dụng trong việc tạo các tùy chọn cho đầu vào đã chọn. Đối với ví dụ này, hãy tạo một đầu vào chọn để chọn một giá trị từ danh sách các loại trái cây

1const options = [
2  {
3    label: "Apple",
4    value: "apple",
5  },
6  {
7    label: "Mango",
8    value: "mango",
9  },
10  {
11    label: "Banana",
12    value: "banana",
13  },
14  {
15    label: "Pineapple",
16    value: "pineapple",
17  },
18];

js

Bạn có thể thắc mắc tại sao cần phải có các đối tượng bên trong mảng khi bạn có thể sử dụng một chuỗi để tạo các tùy chọn. Đó là vì giá trị của tùy chọn không nhất thiết phải giống với nhãn

Ví dụ trên cho thấy cách sử dụng điển hình. Nó được cung cấp một thuộc tính id để cho phép nó được liên kết với một cho mục đích truy cập, cũng như một thuộc tính name để biểu thị tên của điểm dữ liệu được liên kết được gửi tới máy chủ. Mỗi tùy chọn menu được xác định bởi một phần tử

Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

0 được lồng bên trong

Mỗi phần tử

Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

0 phải có một thuộc tính chứa giá trị dữ liệu để gửi tới máy chủ khi tùy chọn đó được chọn. Nếu không bao gồm thuộc tính
Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

3, giá trị sẽ mặc định là văn bản chứa bên trong phần tử. Bạn có thể bao gồm một thuộc tính trên một phần tử
Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

0 để làm cho nó được chọn theo mặc định khi tải trang lần đầu tiên

Phần tử có một số thuộc tính duy nhất mà bạn có thể sử dụng để kiểm soát nó, chẳng hạn như

Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

8 để chỉ định liệu có thể chọn nhiều tùy chọn hay không và
Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

9 để chỉ định số lượng tùy chọn sẽ được hiển thị cùng một lúc. Nó cũng chấp nhận hầu hết các thuộc tính đầu vào của biểu mẫu chung như

  
    Standard controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  
  
    Custom controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  

0,

  
    Standard controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  
  
    Custom controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  

1,

  
    Standard controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  
  
    Custom controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  

2, v.v.

Bạn có thể lồng thêm các phần tử

Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

0 bên trong các phần tử

  
    Standard controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  
  
    Custom controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  

4 để tạo các nhóm tùy chọn riêng biệt bên trong danh sách thả xuống

Để biết thêm ví dụ, xem

Phần tử này bao gồm các thuộc tính toàn cục

Một chuỗi cung cấp gợi ý cho tính năng tự động hoàn thành của tác nhân người dùng. Xem Thuộc tính tự động hoàn thành HTML để biết danh sách đầy đủ các giá trị và chi tiết về cách sử dụng tự động hoàn thành

Thuộc tính Boolean này cho phép bạn chỉ định rằng điều khiển biểu mẫu phải có tiêu điểm đầu vào khi tải trang. Chỉ một thành phần biểu mẫu trong tài liệu có thể có thuộc tính


  
    Standard controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  
  
    Custom controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  

2

Thuộc tính Boolean này chỉ ra rằng người dùng không thể tương tác với điều khiển. Nếu thuộc tính này không được chỉ định, điều khiển sẽ kế thừa cài đặt của nó từ phần tử chứa, ví dụ:


  
    Standard controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  
  
    Custom controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  

9;

Phần tử

body {
  font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
}

.select:focus {
  border-color: blue;
}

html body form fieldset#custom div.select[data-multiple] div.header {
  display: none;
}

html body form fieldset#custom div.select div.header {
  content: "↓";
  display: flex;
  flex: 1;
  align-items: center;
  padding: 0;
  position: relative;
  width: auto;
  box-sizing: border-box;
  border-width: 1px;
  border-style: inherit;
  border-color: inherit;
  border-radius: inherit;
}

html body form fieldset#custom div.select div.header::after {
  content: "↓";
  align-self: stretch;
  display: flex;
  align-content: center;
  justify-content: center;
  justify-items: center;
  align-items: center;
  padding: 0.5em;
}

html body form fieldset#custom div.select div.header:hover::after {
  background-color: blue;
}

.select .header select {
  appearance: none;
  font-family: inherit;
  font-size: inherit;
  padding: 0;
  border-width: 0;
  width: 100%;
  flex: 1;
  display: none;
}

.select .header select optgroup {
  display: none;
}

.select select div.option {
  display: none;
}

html body form fieldset#custom div.select {
  user-select: none;
  box-sizing: border-box;
  position: relative;
  border-radius: 4px;
  border-style: solid;
  border-width: 0;
  border-color: gray;
  width: auto;
  display: inline-block;
}

html body form fieldset#custom div.select:focus,
html body form fieldset#custom div.select:hover {
  border-color: blue;
}

html body form fieldset#custom div.select[data-open] {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

html body form fieldset#custom div.select[data-open] datalist {
  display: initial;
}

html body form fieldset#custom div.select datalist {
  appearance: none;
  position: absolute;
  border-style: solid;
  border-width: 1px;
  border-color: gray;
  left: 0;
  display: none;
  width: 100%;
  box-sizing: border-box;
  z-index: 2;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}

html body form fieldset#custom div.select datalist div.option {
  background-color: white;
  margin-bottom: 1px;
  cursor: pointer;
  padding: 0.5em;
  border-width: 0;
}

html body form fieldset#custom div.select datalist div.option:hover,
html body form fieldset#custom div.select datalist div.option:focus,
html body form fieldset#custom div.select datalist div.option:checked {
  background-color: blue;
  color: white;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-disabled] {
  color: gray;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-checked] {
  background-color: blue;
  color: white;
}

html body form fieldset#custom div.select div.optgroup div.label {
  font-weight: bold;
}

html body form fieldset#custom div.select div.optgroup div.option div.label {
  font-weight: normal;
  padding: 0.25em;
}

html body form fieldset#custom div.select div.header span {
  flex: 1;
  padding: 0.5em;
}
2 để liên kết với [chủ sở hữu biểu mẫu của nó]. Giá trị của thuộc tính này phải là của một
body {
  font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
}

.select:focus {
  border-color: blue;
}

html body form fieldset#custom div.select[data-multiple] div.header {
  display: none;
}

html body form fieldset#custom div.select div.header {
  content: "↓";
  display: flex;
  flex: 1;
  align-items: center;
  padding: 0;
  position: relative;
  width: auto;
  box-sizing: border-box;
  border-width: 1px;
  border-style: inherit;
  border-color: inherit;
  border-radius: inherit;
}

html body form fieldset#custom div.select div.header::after {
  content: "↓";
  align-self: stretch;
  display: flex;
  align-content: center;
  justify-content: center;
  justify-items: center;
  align-items: center;
  padding: 0.5em;
}

html body form fieldset#custom div.select div.header:hover::after {
  background-color: blue;
}

.select .header select {
  appearance: none;
  font-family: inherit;
  font-size: inherit;
  padding: 0;
  border-width: 0;
  width: 100%;
  flex: 1;
  display: none;
}

.select .header select optgroup {
  display: none;
}

.select select div.option {
  display: none;
}

html body form fieldset#custom div.select {
  user-select: none;
  box-sizing: border-box;
  position: relative;
  border-radius: 4px;
  border-style: solid;
  border-width: 0;
  border-color: gray;
  width: auto;
  display: inline-block;
}

html body form fieldset#custom div.select:focus,
html body form fieldset#custom div.select:hover {
  border-color: blue;
}

html body form fieldset#custom div.select[data-open] {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

html body form fieldset#custom div.select[data-open] datalist {
  display: initial;
}

html body form fieldset#custom div.select datalist {
  appearance: none;
  position: absolute;
  border-style: solid;
  border-width: 1px;
  border-color: gray;
  left: 0;
  display: none;
  width: 100%;
  box-sizing: border-box;
  z-index: 2;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}

html body form fieldset#custom div.select datalist div.option {
  background-color: white;
  margin-bottom: 1px;
  cursor: pointer;
  padding: 0.5em;
  border-width: 0;
}

html body form fieldset#custom div.select datalist div.option:hover,
html body form fieldset#custom div.select datalist div.option:focus,
html body form fieldset#custom div.select datalist div.option:checked {
  background-color: blue;
  color: white;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-disabled] {
  color: gray;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-checked] {
  background-color: blue;
  color: white;
}

html body form fieldset#custom div.select div.optgroup div.label {
  font-weight: bold;
}

html body form fieldset#custom div.select div.optgroup div.option div.label {
  font-weight: normal;
  padding: 0.25em;
}

html body form fieldset#custom div.select div.header span {
  flex: 1;
  padding: 0.5em;
}
2 trong cùng một tài liệu. [Nếu thuộc tính này không được đặt, thì phần tử được liên kết với phần tử tổ tiên của nó là
body {
  font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
}

.select:focus {
  border-color: blue;
}

html body form fieldset#custom div.select[data-multiple] div.header {
  display: none;
}

html body form fieldset#custom div.select div.header {
  content: "↓";
  display: flex;
  flex: 1;
  align-items: center;
  padding: 0;
  position: relative;
  width: auto;
  box-sizing: border-box;
  border-width: 1px;
  border-style: inherit;
  border-color: inherit;
  border-radius: inherit;
}

html body form fieldset#custom div.select div.header::after {
  content: "↓";
  align-self: stretch;
  display: flex;
  align-content: center;
  justify-content: center;
  justify-items: center;
  align-items: center;
  padding: 0.5em;
}

html body form fieldset#custom div.select div.header:hover::after {
  background-color: blue;
}

.select .header select {
  appearance: none;
  font-family: inherit;
  font-size: inherit;
  padding: 0;
  border-width: 0;
  width: 100%;
  flex: 1;
  display: none;
}

.select .header select optgroup {
  display: none;
}

.select select div.option {
  display: none;
}

html body form fieldset#custom div.select {
  user-select: none;
  box-sizing: border-box;
  position: relative;
  border-radius: 4px;
  border-style: solid;
  border-width: 0;
  border-color: gray;
  width: auto;
  display: inline-block;
}

html body form fieldset#custom div.select:focus,
html body form fieldset#custom div.select:hover {
  border-color: blue;
}

html body form fieldset#custom div.select[data-open] {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

html body form fieldset#custom div.select[data-open] datalist {
  display: initial;
}

html body form fieldset#custom div.select datalist {
  appearance: none;
  position: absolute;
  border-style: solid;
  border-width: 1px;
  border-color: gray;
  left: 0;
  display: none;
  width: 100%;
  box-sizing: border-box;
  z-index: 2;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}

html body form fieldset#custom div.select datalist div.option {
  background-color: white;
  margin-bottom: 1px;
  cursor: pointer;
  padding: 0.5em;
  border-width: 0;
}

html body form fieldset#custom div.select datalist div.option:hover,
html body form fieldset#custom div.select datalist div.option:focus,
html body form fieldset#custom div.select datalist div.option:checked {
  background-color: blue;
  color: white;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-disabled] {
  color: gray;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-checked] {
  background-color: blue;
  color: white;
}

html body form fieldset#custom div.select div.optgroup div.label {
  font-weight: bold;
}

html body form fieldset#custom div.select div.optgroup div.option div.label {
  font-weight: normal;
  padding: 0.25em;
}

html body form fieldset#custom div.select div.header span {
  flex: 1;
  padding: 0.5em;
}
2, nếu có. ]

Thuộc tính này cho phép bạn liên kết các phần tử với các

body {
  font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
}

.select:focus {
  border-color: blue;
}

html body form fieldset#custom div.select[data-multiple] div.header {
  display: none;
}

html body form fieldset#custom div.select div.header {
  content: "↓";
  display: flex;
  flex: 1;
  align-items: center;
  padding: 0;
  position: relative;
  width: auto;
  box-sizing: border-box;
  border-width: 1px;
  border-style: inherit;
  border-color: inherit;
  border-radius: inherit;
}

html body form fieldset#custom div.select div.header::after {
  content: "↓";
  align-self: stretch;
  display: flex;
  align-content: center;
  justify-content: center;
  justify-items: center;
  align-items: center;
  padding: 0.5em;
}

html body form fieldset#custom div.select div.header:hover::after {
  background-color: blue;
}

.select .header select {
  appearance: none;
  font-family: inherit;
  font-size: inherit;
  padding: 0;
  border-width: 0;
  width: 100%;
  flex: 1;
  display: none;
}

.select .header select optgroup {
  display: none;
}

.select select div.option {
  display: none;
}

html body form fieldset#custom div.select {
  user-select: none;
  box-sizing: border-box;
  position: relative;
  border-radius: 4px;
  border-style: solid;
  border-width: 0;
  border-color: gray;
  width: auto;
  display: inline-block;
}

html body form fieldset#custom div.select:focus,
html body form fieldset#custom div.select:hover {
  border-color: blue;
}

html body form fieldset#custom div.select[data-open] {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

html body form fieldset#custom div.select[data-open] datalist {
  display: initial;
}

html body form fieldset#custom div.select datalist {
  appearance: none;
  position: absolute;
  border-style: solid;
  border-width: 1px;
  border-color: gray;
  left: 0;
  display: none;
  width: 100%;
  box-sizing: border-box;
  z-index: 2;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}

html body form fieldset#custom div.select datalist div.option {
  background-color: white;
  margin-bottom: 1px;
  cursor: pointer;
  padding: 0.5em;
  border-width: 0;
}

html body form fieldset#custom div.select datalist div.option:hover,
html body form fieldset#custom div.select datalist div.option:focus,
html body form fieldset#custom div.select datalist div.option:checked {
  background-color: blue;
  color: white;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-disabled] {
  color: gray;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-checked] {
  background-color: blue;
  color: white;
}

html body form fieldset#custom div.select div.optgroup div.label {
  font-weight: bold;
}

html body form fieldset#custom div.select div.optgroup div.option div.label {
  font-weight: normal;
  padding: 0.25em;
}

html body form fieldset#custom div.select div.header span {
  flex: 1;
  padding: 0.5em;
}
2 ở bất kỳ đâu trong tài liệu, không chỉ bên trong một
body {
  font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
}

.select:focus {
  border-color: blue;
}

html body form fieldset#custom div.select[data-multiple] div.header {
  display: none;
}

html body form fieldset#custom div.select div.header {
  content: "↓";
  display: flex;
  flex: 1;
  align-items: center;
  padding: 0;
  position: relative;
  width: auto;
  box-sizing: border-box;
  border-width: 1px;
  border-style: inherit;
  border-color: inherit;
  border-radius: inherit;
}

html body form fieldset#custom div.select div.header::after {
  content: "↓";
  align-self: stretch;
  display: flex;
  align-content: center;
  justify-content: center;
  justify-items: center;
  align-items: center;
  padding: 0.5em;
}

html body form fieldset#custom div.select div.header:hover::after {
  background-color: blue;
}

.select .header select {
  appearance: none;
  font-family: inherit;
  font-size: inherit;
  padding: 0;
  border-width: 0;
  width: 100%;
  flex: 1;
  display: none;
}

.select .header select optgroup {
  display: none;
}

.select select div.option {
  display: none;
}

html body form fieldset#custom div.select {
  user-select: none;
  box-sizing: border-box;
  position: relative;
  border-radius: 4px;
  border-style: solid;
  border-width: 0;
  border-color: gray;
  width: auto;
  display: inline-block;
}

html body form fieldset#custom div.select:focus,
html body form fieldset#custom div.select:hover {
  border-color: blue;
}

html body form fieldset#custom div.select[data-open] {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

html body form fieldset#custom div.select[data-open] datalist {
  display: initial;
}

html body form fieldset#custom div.select datalist {
  appearance: none;
  position: absolute;
  border-style: solid;
  border-width: 1px;
  border-color: gray;
  left: 0;
  display: none;
  width: 100%;
  box-sizing: border-box;
  z-index: 2;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}

html body form fieldset#custom div.select datalist div.option {
  background-color: white;
  margin-bottom: 1px;
  cursor: pointer;
  padding: 0.5em;
  border-width: 0;
}

html body form fieldset#custom div.select datalist div.option:hover,
html body form fieldset#custom div.select datalist div.option:focus,
html body form fieldset#custom div.select datalist div.option:checked {
  background-color: blue;
  color: white;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-disabled] {
  color: gray;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-checked] {
  background-color: blue;
  color: white;
}

html body form fieldset#custom div.select div.optgroup div.label {
  font-weight: bold;
}

html body form fieldset#custom div.select div.optgroup div.option div.label {
  font-weight: normal;
  padding: 0.25em;
}

html body form fieldset#custom div.select div.header span {
  flex: 1;
  padding: 0.5em;
}
2. Nó cũng có thể ghi đè một phần tử tổ tiên
body {
  font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
}

.select:focus {
  border-color: blue;
}

html body form fieldset#custom div.select[data-multiple] div.header {
  display: none;
}

html body form fieldset#custom div.select div.header {
  content: "↓";
  display: flex;
  flex: 1;
  align-items: center;
  padding: 0;
  position: relative;
  width: auto;
  box-sizing: border-box;
  border-width: 1px;
  border-style: inherit;
  border-color: inherit;
  border-radius: inherit;
}

html body form fieldset#custom div.select div.header::after {
  content: "↓";
  align-self: stretch;
  display: flex;
  align-content: center;
  justify-content: center;
  justify-items: center;
  align-items: center;
  padding: 0.5em;
}

html body form fieldset#custom div.select div.header:hover::after {
  background-color: blue;
}

.select .header select {
  appearance: none;
  font-family: inherit;
  font-size: inherit;
  padding: 0;
  border-width: 0;
  width: 100%;
  flex: 1;
  display: none;
}

.select .header select optgroup {
  display: none;
}

.select select div.option {
  display: none;
}

html body form fieldset#custom div.select {
  user-select: none;
  box-sizing: border-box;
  position: relative;
  border-radius: 4px;
  border-style: solid;
  border-width: 0;
  border-color: gray;
  width: auto;
  display: inline-block;
}

html body form fieldset#custom div.select:focus,
html body form fieldset#custom div.select:hover {
  border-color: blue;
}

html body form fieldset#custom div.select[data-open] {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

html body form fieldset#custom div.select[data-open] datalist {
  display: initial;
}

html body form fieldset#custom div.select datalist {
  appearance: none;
  position: absolute;
  border-style: solid;
  border-width: 1px;
  border-color: gray;
  left: 0;
  display: none;
  width: 100%;
  box-sizing: border-box;
  z-index: 2;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}

html body form fieldset#custom div.select datalist div.option {
  background-color: white;
  margin-bottom: 1px;
  cursor: pointer;
  padding: 0.5em;
  border-width: 0;
}

html body form fieldset#custom div.select datalist div.option:hover,
html body form fieldset#custom div.select datalist div.option:focus,
html body form fieldset#custom div.select datalist div.option:checked {
  background-color: blue;
  color: white;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-disabled] {
  color: gray;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-checked] {
  background-color: blue;
  color: white;
}

html body form fieldset#custom div.select div.optgroup div.label {
  font-weight: bold;
}

html body form fieldset#custom div.select div.optgroup div.option div.label {
  font-weight: normal;
  padding: 0.25em;
}

html body form fieldset#custom div.select div.header span {
  flex: 1;
  padding: 0.5em;
}
2

Thuộc tính Boolean này chỉ ra rằng có thể chọn nhiều tùy chọn trong danh sách. Nếu nó không được chỉ định, thì chỉ có thể chọn một tùy chọn tại một thời điểm. Khi

Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

8 được chỉ định, hầu hết các trình duyệt sẽ hiển thị hộp danh sách cuộn thay vì một dòng thả xuống

Thuộc tính này được sử dụng để chỉ định tên của điều khiển

Một thuộc tính Boolean cho biết rằng phải chọn một tùy chọn có giá trị chuỗi không trống

Nếu điều khiển được trình bày dưới dạng hộp danh sách cuộn [e. g. khi

Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

8 được chỉ định], thuộc tính này biểu thị số hàng trong danh sách sẽ hiển thị cùng một lúc. Trình duyệt không bắt buộc phải trình bày phần tử được chọn dưới dạng hộp danh sách cuộn. Giá trị mặc định là
const selects = custom.querySelectorAll['select'];
for [const select of selects] {
  const div = document.createElement['div'];
  const header = document.createElement['div'];
  const datalist = document.createElement['datalist'];
  const optgroups = select.querySelectorAll['optgroup'];
  const span = document.createElement['span'];
  const options = select.options;
  const parent = select.parentElement;
  const multiple = select.hasAttribute['multiple'];
  function  title[e] {
    const disabled = this.hasAttribute['data-disabled'];
    select.value = this.dataset.value;
    span.innerText = this.dataset.label;
    if [disabled] return;
    if [multiple] {
      if [e.shiftKey] {
        const checked = this.hasAttribute["data-checked"];
        if [checked] {
          this.removeAttribute["data-checked"];
        } else {
          this.setAttribute["data-checked", ""];
        }
      } else {
        const options = div.querySelectorAll['.option'];
        for [let i = 0; i  {
    if [!multiple] {
      const open = this.hasAttribute["data-open"];
      e.stopPropagation[];
      if [open] {
        div.removeAttribute["data-open"];
      } else {
        div.setAttribute["data-open", ""];
      }
    }
  };

  div.onkeyup = [event] => {
    event.preventDefault[];
    if [event.keyCode === 13] {
      div.click[];
    }
  };

  document.addEventListener['click', [e] => {
    if [div.hasAttribute["data-open"]] {
      div.removeAttribute["data-open"];
    }
  }];

  const width = Math.max[...Array.from[options].map[[e] => {
    span.innerText = e.label;
    return div.offsetWidth;
  }]];

  console.log[width];
  div.style.width = `${width}px`;
}
document.forms[0].onsubmit = [e] => {
  const data = new FormData[this];
  e.preventDefault[];
  submit.innerText = JSON.stringify[[...data.entries[]]];
};
8

Ghi chú. Theo đặc tả HTML, giá trị mặc định cho kích thước phải là

const selects = custom.querySelectorAll['select'];
for [const select of selects] {
  const div = document.createElement['div'];
  const header = document.createElement['div'];
  const datalist = document.createElement['datalist'];
  const optgroups = select.querySelectorAll['optgroup'];
  const span = document.createElement['span'];
  const options = select.options;
  const parent = select.parentElement;
  const multiple = select.hasAttribute['multiple'];
  function  title[e] {
    const disabled = this.hasAttribute['data-disabled'];
    select.value = this.dataset.value;
    span.innerText = this.dataset.label;
    if [disabled] return;
    if [multiple] {
      if [e.shiftKey] {
        const checked = this.hasAttribute["data-checked"];
        if [checked] {
          this.removeAttribute["data-checked"];
        } else {
          this.setAttribute["data-checked", ""];
        }
      } else {
        const options = div.querySelectorAll['.option'];
        for [let i = 0; i  {
    if [!multiple] {
      const open = this.hasAttribute["data-open"];
      e.stopPropagation[];
      if [open] {
        div.removeAttribute["data-open"];
      } else {
        div.setAttribute["data-open", ""];
      }
    }
  };

  div.onkeyup = [event] => {
    event.preventDefault[];
    if [event.keyCode === 13] {
      div.click[];
    }
  };

  document.addEventListener['click', [e] => {
    if [div.hasAttribute["data-open"]] {
      div.removeAttribute["data-open"];
    }
  }];

  const width = Math.max[...Array.from[options].map[[e] => {
    span.innerText = e.label;
    return div.offsetWidth;
  }]];

  console.log[width];
  div.style.width = `${width}px`;
}
document.forms[0].onsubmit = [e] => {
  const data = new FormData[this];
  e.preventDefault[];
  submit.innerText = JSON.stringify[[...data.entries[]]];
};
9;

Trên máy tính để bàn, có một số cách để chọn nhiều tùy chọn trong phần tử có thuộc tính

Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

8

Người dùng chuột có thể giữ các phím Ctrl, Command hoặc Shift [tùy thuộc vào ý nghĩa của hệ điều hành của bạn] rồi nhấp vào nhiều tùy chọn để chọn/bỏ chọn chúng

Cảnh báo. Cơ chế chọn nhiều mục không liền kề qua bàn phím được mô tả dưới đây dường như chỉ hoạt động trong Firefox

Trên macOS, các phím tắt Ctrl + Lên và Ctrl + Xuống xung đột với các phím tắt mặc định của hệ điều hành cho các cửa sổ Ứng dụng và Điều khiển nhiệm vụ, vì vậy bạn sẽ phải tắt các phím tắt này trước khi phím tắt hoạt động

Người dùng bàn phím có thể chọn nhiều mục liền kề bằng cách

  • Tập trung vào yếu tố [e. g. sử dụng Tab ]
  • Chọn một mục ở trên cùng hoặc dưới cùng của phạm vi mà họ muốn chọn bằng cách sử dụng các phím con trỏ Lên và Xuống để đi lên và xuống các tùy chọn
  • Giữ phím Shift và sau đó sử dụng các phím con trỏ Lên và Xuống để tăng hoặc giảm phạm vi các mục đã chọn

Người dùng bàn phím có thể chọn nhiều mục không liền kề bằng cách

  • Tập trung vào yếu tố [e. g. sử dụng Tab ]
  • Giữ phím Ctrl rồi sử dụng các phím con trỏ Lên và Xuống để thay đổi tùy chọn chọn "tiêu điểm", tôi. e. cái sẽ được chọn nếu bạn chọn làm như vậy. Tùy chọn chọn "tiêu điểm" được đánh dấu bằng đường viền chấm, giống như cách liên kết tập trung vào bàn phím
  • Nhấn Space để chọn/bỏ chọn các tùy chọn chọn "tiêu điểm"

Phần tử nổi tiếng là khó tạo kiểu hiệu quả bằng CSS. Bạn có thể tác động đến một số khía cạnh như bất kỳ yếu tố nào — ví dụ: thao tác với mô hình hộp, phông chữ được hiển thị, v.v. , và bạn có thể sử dụng thuộc tính 6 để xóa hệ thống mặc định 6

Tuy nhiên, các thuộc tính này không tạo ra kết quả nhất quán trên các trình duyệt và rất khó để thực hiện những việc như sắp xếp các loại phần tử biểu mẫu khác nhau trong một cột với nhau. Cấu trúc bên trong của phần tử rất phức tạp và khó kiểm soát. Nếu bạn muốn có toàn quyền kiểm soát, bạn nên cân nhắc sử dụng một thư viện có các phương tiện tốt để tạo kiểu dáng cho các tiện ích biểu mẫu hoặc thử cuộn menu thả xuống của riêng bạn bằng cách sử dụng các phần tử phi ngữ nghĩa, JavaScript và WAI-ARIA để cung cấp ngữ nghĩa

Để biết thêm thông tin hữu ích về kiểu dáng , hãy xem

  • Tạo kiểu biểu mẫu HTML
  • Kiểu dáng nâng cao cho biểu mẫu HTML

Ngoài ra, hãy xem ví dụ "Tùy chỉnh kiểu chọn" bên dưới để biết ví dụ về việc bạn có thể thử tạo kiểu đơn giản

Ví dụ sau tạo một menu thả xuống rất đơn giản, tùy chọn thứ hai được chọn theo mặc định



  First Value
  Second Value
  Third Value

Ví dụ sau đây phức tạp hơn, hiển thị nhiều tính năng hơn mà bạn có thể sử dụng trên phần tử ______ 46 ______

Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

bạn sẽ thấy rằng

  • Có thể chọn nhiều tùy chọn vì chúng tôi đã bao gồm thuộc tính
    Please choose one or more pets:
      
        
          Dog
          Cat
          Hamster
        
        
          Parrot
          Macaw
          Albatross
        
      
    
    
    8
  • Thuộc tính
    Please choose one or more pets:
      
        
          Dog
          Cat
          Hamster
        
        
          Parrot
          Macaw
          Albatross
        
      
    
    
    9 chỉ hiển thị 4 dòng tại một thời điểm;
  • Chúng tôi đã bao gồm các yếu tố
    
      
        Standard controls
        
          Carrots
          Peas
          Beans
          Pneumonoultramicroscopicsilicovolcanoconiosis
        
      
      
        Custom controls
        
          Carrots
          Peas
          Beans
          Pneumonoultramicroscopicsilicovolcanoconiosis
        
      
    
    
    4 để chia các tùy chọn thành các nhóm khác nhau. Đây là một nhóm hoàn toàn trực quan, trực quan hóa của nó thường bao gồm tên nhóm được in đậm và các tùy chọn được thụt vào
  • Tùy chọn "Hamster" bao gồm thuộc tính
    
      
        Standard controls
        
          Carrots
          Peas
          Beans
          Pneumonoultramicroscopicsilicovolcanoconiosis
        
      
      
        Custom controls
        
          Carrots
          Peas
          Beans
          Pneumonoultramicroscopicsilicovolcanoconiosis
        
      
    
    
    1 và do đó không thể chọn được

Ví dụ này cho thấy cách bạn có thể sử dụng một số CSS và JavaScript để cung cấp kiểu dáng tùy chỉnh mở rộng cho hộp

Ví dụ này về cơ bản

  • Sao chép bối cảnh của [các
    Please choose one or more pets:
      
        
          Dog
          Cat
          Hamster
        
        
          Parrot
          Macaw
          Albatross
        
      
    
    
    0] trong trình bao bọc gốc và thực hiện lại hành vi dự kiến ​​tiêu chuẩn bằng cách sử dụng các phần tử HTML và JavaScript bổ sung. Điều này bao gồm hành vi tab cơ bản để cung cấp khả năng truy cập bàn phím
  • Ánh xạ một số tiêu chuẩn gốc từ 9 đến id0 của các phần tử mới để quản lý trạng thái và CSS

Ghi chú. Không phải tất cả các tính năng gốc đều được hỗ trợ, đó là Bằng chứng về khái niệm. CNTT bắt đầu từ HTML tiêu chuẩn nhưng có thể đạt được kết quả tương tự khi bắt đầu từ dữ liệu JSON, HTML tùy chỉnh hoặc các giải pháp khác

HTML


  
    Standard controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  
  
    Custom controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  

CSS

body {
  font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
}

.select:focus {
  border-color: blue;
}

html body form fieldset#custom div.select[data-multiple] div.header {
  display: none;
}

html body form fieldset#custom div.select div.header {
  content: "↓";
  display: flex;
  flex: 1;
  align-items: center;
  padding: 0;
  position: relative;
  width: auto;
  box-sizing: border-box;
  border-width: 1px;
  border-style: inherit;
  border-color: inherit;
  border-radius: inherit;
}

html body form fieldset#custom div.select div.header::after {
  content: "↓";
  align-self: stretch;
  display: flex;
  align-content: center;
  justify-content: center;
  justify-items: center;
  align-items: center;
  padding: 0.5em;
}

html body form fieldset#custom div.select div.header:hover::after {
  background-color: blue;
}

.select .header select {
  appearance: none;
  font-family: inherit;
  font-size: inherit;
  padding: 0;
  border-width: 0;
  width: 100%;
  flex: 1;
  display: none;
}

.select .header select optgroup {
  display: none;
}

.select select div.option {
  display: none;
}

html body form fieldset#custom div.select {
  user-select: none;
  box-sizing: border-box;
  position: relative;
  border-radius: 4px;
  border-style: solid;
  border-width: 0;
  border-color: gray;
  width: auto;
  display: inline-block;
}

html body form fieldset#custom div.select:focus,
html body form fieldset#custom div.select:hover {
  border-color: blue;
}

html body form fieldset#custom div.select[data-open] {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

html body form fieldset#custom div.select[data-open] datalist {
  display: initial;
}

html body form fieldset#custom div.select datalist {
  appearance: none;
  position: absolute;
  border-style: solid;
  border-width: 1px;
  border-color: gray;
  left: 0;
  display: none;
  width: 100%;
  box-sizing: border-box;
  z-index: 2;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}

html body form fieldset#custom div.select datalist div.option {
  background-color: white;
  margin-bottom: 1px;
  cursor: pointer;
  padding: 0.5em;
  border-width: 0;
}

html body form fieldset#custom div.select datalist div.option:hover,
html body form fieldset#custom div.select datalist div.option:focus,
html body form fieldset#custom div.select datalist div.option:checked {
  background-color: blue;
  color: white;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-disabled] {
  color: gray;
}

html
  body
  form
  fieldset#custom
  div.select
  div.optgroup
  div.option[data-checked] {
  background-color: blue;
  color: white;
}

html body form fieldset#custom div.select div.optgroup div.label {
  font-weight: bold;
}

html body form fieldset#custom div.select div.optgroup div.option div.label {
  font-weight: normal;
  padding: 0.25em;
}

html body form fieldset#custom div.select div.header span {
  flex: 1;
  padding: 0.5em;
}

JavaScript

const selects = custom.querySelectorAll['select'];
for [const select of selects] {
  const div = document.createElement['div'];
  const header = document.createElement['div'];
  const datalist = document.createElement['datalist'];
  const optgroups = select.querySelectorAll['optgroup'];
  const span = document.createElement['span'];
  const options = select.options;
  const parent = select.parentElement;
  const multiple = select.hasAttribute['multiple'];
  function  title[e] {
    const disabled = this.hasAttribute['data-disabled'];
    select.value = this.dataset.value;
    span.innerText = this.dataset.label;
    if [disabled] return;
    if [multiple] {
      if [e.shiftKey] {
        const checked = this.hasAttribute["data-checked"];
        if [checked] {
          this.removeAttribute["data-checked"];
        } else {
          this.setAttribute["data-checked", ""];
        }
      } else {
        const options = div.querySelectorAll['.option'];
        for [let i = 0; i  {
    if [!multiple] {
      const open = this.hasAttribute["data-open"];
      e.stopPropagation[];
      if [open] {
        div.removeAttribute["data-open"];
      } else {
        div.setAttribute["data-open", ""];
      }
    }
  };

  div.onkeyup = [event] => {
    event.preventDefault[];
    if [event.keyCode === 13] {
      div.click[];
    }
  };

  document.addEventListener['click', [e] => {
    if [div.hasAttribute["data-open"]] {
      div.removeAttribute["data-open"];
    }
  }];

  const width = Math.max[...Array.from[options].map[[e] => {
    span.innerText = e.label;
    return div.offsetWidth;
  }]];

  console.log[width];
  div.style.width = `${width}px`;
}
document.forms[0].onsubmit = [e] => {
  const data = new FormData[this];
  e.preventDefault[];
  submit.innerText = JSON.stringify[[...data.entries[]]];
};

Kết quả

Danh mục nội dung, , , , , và phần tửNội dung được phépKhông hoặc nhiều hơn

Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

0 hoặc

  
    Standard controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  
  
    Custom controls
    
      Carrots
      Peas
      Beans
      Pneumonoultramicroscopicsilicovolcanoconiosis
    
  

4 phần tử. Bỏ sót thẻKhông có, cả thẻ bắt đầu và thẻ kết thúc đều bắt buộc. Cha mẹ được phép Bất kỳ phần tử nào chấp nhận. Vai trò ARIA ngầm địnhid3 không có thuộc tính
Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

8 và không có thuộc tính
Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

9 nào lớn hơn 1, nếu không thì id6Các vai trò ARIA được phépid7 không có thuộc tính
Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

8 và không có thuộc tính
Please choose one or more pets:
  
    
      Dog
      Cat
      Hamster
    
    
      Parrot
      Macaw
      Albatross
    
  

9 nào lớn hơn 1, nếu không thì không có giao diện 0 cho phépDOM_______48_______1

Thẻ chọn có giá trị không?

Thẻ select trong HTML được sử dụng để tạo danh sách thả xuống gồm các tùy chọn có thể được chọn. Thẻ tùy chọn chứa giá trị sẽ được sử dụng khi được chọn . Giá trị mặc định của phần tử được chọn có thể được đặt bằng cách sử dụng thuộc tính 'được chọn' trên tùy chọn bắt buộc. Đây là một thuộc tính boolean.

Giá trị mặc định cho thẻ chọn là gì?

Giá trị mặc định là 0 .

Làm cách nào để lấy giá trị đã chọn trong danh sách thả xuống bằng JavaScript?

Nhận giá trị được chọn thả xuống bằng JavaScript .
chức năng GetMaster1Details[]
var value = document. getElementById[""];.
var getvalue = giá trị. tùy chọn [giá trị. mục lục được chọn]. giá trị;
var gettext = giá trị. tùy chọn [giá trị. mục lục được chọn]. .
cảnh báo ["giá trị. -" +" "+ getvalue + " "+ "Văn bản. -" +" "+ gettext];

Chủ Đề