-3
只有當我在on change事件中有代碼時,列表纔會被排序。我希望它在頁面加載時排序。我嘗試過使用onload,但沒有成功。排序不排序javascript
此外,我試圖讓它,無論用戶選擇,該值是列表中的第一個。
作品...
myDropdown.onchange = function() {
$("#dropdown").append($("#dropdown option").sort(function(a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
}))
myTextBox.value = this.value;
}
不工作...
// var myDropdown = $('#dropdown');
// var opts_list = myDropdown.find('option');
// function sortTheList() {
// $("#dropdown").append($("#dropdown option").sort(function(a, b) {
// return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
// }))
// };
// myDropdown.onload = function() {
// $('#dropdown option').sort(function(a,b) { return $(a).text() > $(b).text() ? 1 : -1; });
// myDropdown.html('').append('#dropdown option');
// }
// myDropdown.onload = function() {
// $("#dropdown").on($("#dropdown option").sort(function(a, b) {
// return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
// }))
// }
'myDropdown'是一個jQuery對象。它沒有'onload',但是你可以做'$(document).ready(function(){...});' – 4castle
看看這個http://stackoverflow.com/questions/33757017 /下拉的JavaScript-的onload?RQ = 1 – aggaton