項目中要實現一個select選擇器選擇後跳轉url,並保存selected的值。html
url是用get來傳遞參數,因此考慮加載新頁面時,讀取參數值,並賦值到select中。jquery
可是因爲url的參數使用的是中文,select不識別,因此經過jquery現成的轉碼函數,一句話搞定~!函數
select選中值的防刷新: 每次加載頁面後讀取url中的參數值,而後設定select的選中值,因爲url中包含中文,使用了jquery的解碼函數, var myurl=new LG.URL(window.location.href);//js封裝的url操做函數 $("#yewu").val(decodeURIComponent(myurl.get("yewu")));//jquery解碼函數
Encode URL String <script> var url = $(location).attr('href'); //get current url //OR var url = 'folder/index.html?param=#23dd&noob=yes'; //or specify one var encodedUrl = encodeURIComponent(url); console.log(encodedUrl); //outputs folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes </script> Decode URL String <script> var url = $(location).attr('href'); //get current url //OR var url = 'folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes'; //or specify one var decodedUrl = decodeURIComponent(url); console.log(decodedUrl); //outputs folder/index.html?param=#23dd&noob=yes </script>