How to save the selected field in combobox after refreshing the page?
I'm working in mvc3 and 开发者_Go百科did this thing to change the language by url address (it work great):
var urlString = window.location.host; //the url with localhost:XXX only -and if it changes it will adjust itself
var Lang = $(this)[0].value; //en or fr
window.location = "http://" + urlString + "/" + Lang;
but now I have a serious problem: window.location - refreshes the page, and the $(this)[0].value - returns to be as the beginning. for example if I change the combobox from "english" to "french", the language does change to french (coz the url isnt refreshed), but in the combobox, the selected field is "english" again and I cant change it because all the page is refreshed. so...is anybody can tell me what to do??
thank in advance.
In your controller, I guess you're passing in the language as a parameter, set the selected value of your dropdown to the language passed in so that when the page is loaded the value is set and the correct item is selected.
If this is a refresh it is a complete reload of the page so you need to save state. Either investigate html 5's local storage (limited browser support) http://www.w3schools.com/html5/html5_webstorage.asp
or when its selected save it in a preference cookie in javascript. The code is already mostly written for you here - you'll just need to read it on page load and save it when the combo box changes.
Javascript remember combobox value
精彩评论