Tôi có đoạn code sauLấy giá trị được chọn trong một thẻ multiselect trong Javascript
function searchFlights() {
var select1 = document.getElementById("airports-select-1");
var selected1 = [];
while(select1.selectedIndex != -1) {
if(select1.selectedIndex != 0) selected1.push(select1.options[select1.selectedIndex].value);
select1.options[select1.selectedIndex].selected = false;
}
console.log(selected1);
}
này hoạt động đúng, nhưng như bạn có thể nhìn thấy từ các mã dòng này:
select1.options[select1.selectedIndex].selected = false;
là làm một bỏ chọn giá trị.
Bây giờ, tôi không muốn bỏ chọn các giá trị. Nếu tôi bỏ ghi chú dòng đó trong mã, mã sẽ chạy mãi mãi.
Có giải pháp tinh vi và tinh vi hơn nào để truy xuất nhiều giá trị từ thẻ được chọn bằng Javascript không?
Đó là câu trả lời. – aurora