Reputation: 145
I have 4 dynamically dependent select menus. The menus correspond to state, county, area, neighborhood. The dynamic dependence is achieved through a jQuery script. What I want to accomplish is to make the menus remember the last option selected by a user for each menu. So I thought that if I can grab the url parameters that would do the trick. So I modified the script in this way....
getCounty: function (results) {
if(!results) return;
var allCounties = $("<option value=\"All\">All Counties </option>");
counties.html(results);
counties.prepend(allCounties);
var w = $.query.get("county_id");
if(w) {
counties.val(w).attr('selected', true);
} else {
counties.val("All").attr('selected', true);
}
Is the $.query.get
function correct? Is there anything else wrong with my code? I have to admit I am not a jQuery expert...The code works just fine if I type
counties.val(3).attr('selected',true)
The third option of the select menu gets selected.
I resolved the problem by using
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
And after
var county = getUrlVars()["county_id"];
if (county)
counties.val(county).attr('selected',true);
else
counties.val("All").attr('selected',true);
Upvotes: 0
Views: 171
Reputation: 756
I dont know what is "county_id", but i supose is a other menu, so maybe better :
getCounty: function (results) {
if(!results) return;
var allCounties = $("<option value=\"All\">All Counties </option>");
counties.html(results);
counties.append(allCounties); ///The all option first
var w = $('#county_id :selected').val(); ///w is the value of option selected of other combo
counties.find('option[value='+w+']').attr('selected', 'selected'); ///this find a option with value W, if exist mark like selected
}
Upvotes: 1