-1
これは完全に解決可能な/簡単な問題のように思えるが、私は解決策を見つけるのに苦労している。新たに選択されたhtmlオプションをページロードの最初の選択にするには?
ユーザーがドロップダウンリストからオプションを選択してオプションを保存した後(それらのページがリダイレクトされている)、そのドロップダウンリストに戻り、彼らが選択したオプションはトップオプションですか?
今のところ、リストは「デフォルト」オプションが最初に選択され、残りはアルファベット順にソートされます。次に、ユーザーが別のオプションを選択した場合、リストはソートされて最上位のオプションになり、残りはアルファベット順にソートされます。ページの更新時にその注文を維持するにはどうすればよいですか?あなたは、その唯一のクライアント側のJavaScriptた場合にクッキーを使用する必要があります
JS
// Below function: This sorts the html dropdown list so that the top option is the selected option.
document.addEventListener("DOMContentLoaded", function(event) {
$(document).ready(function(){
$(document).on('change', '#dropdown', function(){
$(this).html($('option', this).sort(function(a,b){
if(a.text == b.text) {
return 0;
}
if(a.text > b.text) {
return 1;
} else {
return -1;
}
}));
var nSel = $('option:selected', this).detach();
$(this).prepend(nSel);
// $('select').prepend(nSel);
$("select").val(nSel);
myDropdown.value = myTextBox.value;
});
});
})
HTML /小枝
<div class = "dropdowns">
<select id = "dropdown">
<option> name </option>
{% for key, value in Infos|sort %}
<option value="{{ key }} | {{ value }}"> {{ key }} | {{ value }} </option>
{% endfor %}
</select>
</div>
なぜあなたはそれをしたいですか? –