Làm cách nào để có được chỉ mục thả xuống đã chọn trong JavaScript?
Nhận giá trị của một lựa chọn trong HTML là một câu hỏi khá lặp đi lặp lại. Tìm hiểu cách trả về giá trị và văn bản của danh sách thả xuống bằng JavaScript hoặc jQuery thuần túy Show
Giả sử bạn có đoạn mã sau
Có hai mẩu thông tin chúng tôi có thể muốn nhận được trong trường hợp này
Cách lấy giá trị của một lựa chọnĐể lấy giá trị của một lựa chọn hoặc danh sách thả xuống trong HTML bằng cách sử dụng JavaScript thuần túy, trước tiên, chúng tôi lấy thẻ chọn, trong trường hợp này là theo id, sau đó chúng tôi lấy giá trị đã chọn thông qua thuộc tính selectIndex
Giá trị "en" sẽ được in trên bảng điều khiển (Ctrl + Shift + J để mở bảng điều khiển) Lấy giá trị của một lựa chọn với jQuery
Cách lấy văn bản của một lựa chọnĐể lấy nội dung của một tùy chọn, nhưng không lấy giá trị, mã gần như giống nhau, chỉ cần lấy thuộc tính văn bản thay vì giá trị selectElement.options[selectElement.selectedIndex].value6 là một selectElement.options[selectElement.selectedIndex].value7 phản ánh chỉ mục của phần tử selectElement.options[selectElement.selectedIndex].value8 được chọn đầu tiên hoặc cuối cùng, tùy thuộc vào giá trị của selectElement.options[selectElement.selectedIndex].value9. Giá trị 00 cho biết không có phần tử nào được chọnMột số
Bảng BCD chỉ tải trong trình duyệt có bật JavaScript. Bật JavaScript để xem dữ liệu Tìm thấy một vấn đề với trang này?
Sửa đổi lần cuối. Ngày 9 tháng 9 năm 2022, bởi những người đóng góp MDN Thuộc tính danh sách thả xuống được chọnChỉ mục trong HTML DOM được đặt hoặc trả về chỉ mục của tùy chọn đã chọn trong danh sách thả xuống
Chỉ mục bắt đầu từ 0 và nếu không có phần tử nào được chọn thì nó trả về -1. Nếu danh sách thả xuống cho phép nhiều lựa chọn, nó sẽ chỉ trả về chỉ mục của tùy chọn đầu tiên được chọn Trong bài viết này, chúng ta sẽ học cách lấy các giá trị đã chọn trong dropdown list trong Javascript. Chúng ta có thể lấy các giá trị bằng 2 phương thức
Chúng ta sẽ hiểu cả hai phương pháp này thông qua các ví dụ Phương pháp 1. Sử dụng thuộc tính giá trị. The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the cú pháp selectElement.value Ví dụ. Ví dụ này mô tả thuộc tính giá trị có thể được tìm thấy cho các phần tử đã chọn HTML 01 02________ 103 ________ 104 ________ 105 ________ 106 ________ 103 ________ 108 ________ 105 selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value2 selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value4 06selectElement.options[selectElement.selectedIndex].value6 08 05selectElement.options[selectElement.selectedIndex].value6____104____105 02________ 103 ________ 1014 ________ 105 06 03____1018 019 020 021 05selectElement.options[selectElement.selectedIndex].value1 024________ 106 ________ 66 ________ 1018 ________ 1028 06 03____1031 05selectElement.options[selectElement.selectedIndex].value1 034selectElement.options[selectElement.selectedIndex].value1 036________ 106 ________ 66 ________ 1031 ________ 105 ________ 106 ________ 103 ________ 1043 ________ 1044 selectElement.options[selectElement.selectedIndex].value1 03____1047 048 020 050 05 052 03 054 055 020 057 058 054 05 052 03 054 055 020 066 067 054 05 052 03 054 055 020 035 036 054 05selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value6 047 05________ 106 ________ 66 ________ 1043 ________ 105 02 02________ 106 ________ 103 ________ 1043 ________ 602 selectElement.options[selectElement.selectedIndex].value1 03selectElement.options[selectElement.selectedIndex].value05 selectElement.options[selectElement.selectedIndex].value06 020selectElement.options[selectElement.selectedIndex].value08 selectElement.options[selectElement.selectedIndex].value09 selectElement.options[selectElement.selectedIndex].value05 05________ 106 ________ 66 ________ 1043 ________ 105 02 02 06 03selectElement.options[selectElement.selectedIndex].value20 selectElement.options[selectElement.selectedIndex].value21 020selectElement.options[selectElement.selectedIndex].value23 selectElement.options[selectElement.selectedIndex].value24 selectElement.options[selectElement.selectedIndex].value20 05selectElement.options[selectElement.selectedIndex].value27 ________ 106 03selectElement.options[selectElement.selectedIndex].value30 ________ 631 020selectElement.options[selectElement.selectedIndex].value33 _______ 105 06selectElement.options[selectElement.selectedIndex].value36 selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value38 selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value40 selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value42 06selectElement.options[selectElement.selectedIndex].value44 06selectElement.options[selectElement.selectedIndex].value6 selectElement.options[selectElement.selectedIndex].value30 05selectElement.options[selectElement.selectedIndex].value6____1014 05 02________ 66 ________ 654 ________ 105 đầu ra giá trị tài sản Phương pháp 2. Sử dụng thuộc tính selectIndex với thuộc tính tùy chọn The selectedIndex property returns the index of the currently selected element in the dropdown list. This index starts from 0 and returns -1 if no option is selected. The options property returns the collection of all the option elements in the cú pháp selectElement.options[selectElement.selectedIndex].value Giá trị tài sản
Return value: It returns HTMLOptionsCollection Object by specifying all the Ví dụ. Ví dụ này mô tả thuộc tính selectIndex với thuộc tính tùy chọn HTML 01________ 103 ________ 104 ________ 105 ________ 106 ________ 103 ________ 108 ________ 105 selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value2 selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value4 06selectElement.options[selectElement.selectedIndex].value6 08 05selectElement.options[selectElement.selectedIndex].value6____104____105 02________ 103 ________ 1014 ________ 105 06 03____1018 019 020 021 05selectElement.options[selectElement.selectedIndex].value1 024________ 106 ________ 66 ________ 1018 ________ 1028 06 03____1031 05selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value2 selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value4 ________ 106 ________ 66 ________ 1031 ________ 105 ________ 106 ________ 103 ________ 1043 ________ 1044 selectElement.options[selectElement.selectedIndex].value1 03____1047 048 020 050 05 052 03 054 055 020 057 058 054 05 052 03 054 055 020 066 067 054 05 052 03 054 055 020 035 036 054 05selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value6 047 05________ 106 ________ 66 ________ 1043 ________ 105 ________ 106 ________ 103 ________ 1043 ________ 602 selectElement.options[selectElement.selectedIndex].value1 03selectElement.options[selectElement.selectedIndex].value05 selectElement.options[selectElement.selectedIndex].value06 020selectElement.options[selectElement.selectedIndex].value08____609 selectElement.options[selectElement.selectedIndex].value05 028________ 106 ________ 66 ________ 1043 ________ 105 02 02 06 03selectElement.options[selectElement.selectedIndex].value20 selectElement.options[selectElement.selectedIndex].value21 020selectElement.options[selectElement.selectedIndex].value23 0175selectElement.options[selectElement.selectedIndex].value20 05________ 106 03selectElement.options[selectElement.selectedIndex].value30 ________ 631 020selectElement.options[selectElement.selectedIndex].value33 _______ 105 02 06selectElement.options[selectElement.selectedIndex].value36 selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value38 _______61____10191 selectElement.options[selectElement.selectedIndex].value1 selectElement.options[selectElement.selectedIndex].value42 06selectElement.options[selectElement.selectedIndex].value44 06selectElement.options[selectElement.selectedIndex].value6 selectElement.options[selectElement.selectedIndex].value30 05selectElement.options[selectElement.selectedIndex].value6____1014 05 02________ 66 ________ 654 ________ 105 đầu ra Thuộc tính SelectedIndex với thuộc tính tùy chọn JavaScript được biết đến nhiều nhất để phát triển trang web nhưng nó cũng được sử dụng trong nhiều môi trường không có trình duyệt. Bạn có thể học JavaScript từ đầu bằng cách làm theo Hướng dẫn JavaScript và Ví dụ về JavaScript này Làm cách nào để lấy chỉ mục đã chọn trong JavaScript?Chọn Thuộc tính Chỉ mục đã chọn . Select the Bỏ chọn tất cả các tùy chọn. getElementById("mySelect"). đã chọn Index = "-1"; Thuộc tính selectIndex sẽ trả về "-1" nếu không có tùy chọn nào được chọn. getElementById("mySelect"). mục lục được chọn; Làm cách nào để lấy văn bản đã chọn từ danh sách thả xuống trong JavaScript?tài liệu. getElementById('gửi'). onclick = chức năng () { var e = tài liệu. getElementById("thú cưng"); văn bản var = e. tùy chọn [e. mục lục được chọn]. chữ; tài liệu. getElementById("thùng chứa"). innerHTML = 'Văn bản được chọn là ' + văn bản; Làm cách nào để nhận giá trị từ điều khiển chọn thả xuống trong JavaScript?Nhận giá trị được chọn thả xuống bằng JavaScript . chức năng GetMaster1Details() var value = document. getElementById("<%=ddlMaster1. ClientID%>");. 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); Làm cách nào để lấy giá trị đã chọn từ thẻ chọn trong JavaScript?Phương thức val() trả về giá trị của giá trị thuộc tính đã chọn . để lựa chọnValue = $("#selectVal tùy chọn. đã chọn"). giá trị(); . |