2016-05-17 7 views
-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> 
+0

なぜあなたはそれをしたいですか? –

答えて

0

はこちらをご覧ください:http://www.w3schools.com/js/js_cookies.asp

を要約すると:あなたはクッキー文字列を設定する必要があります。

document.cookie = "selectedOption=" + option; 

手動で文字列を分割し、キー/値を解析します。

関連する問題