I'm making a .get
request to the ipinfo API to obtain the country code, then lowercase the string and parse it to a second function which should pickup the country code (example: us) and display the flag based on ISO 3166-1 alpha-2.
Although countrySelect()
seems to be ignoring my country variable.
A default country gets set on page load ('gb'), but then my callback should overwrite that.
Any idea of what's going on?
var country;
$.get("https://ipinfo.io/country", function(data) {
var usercountry = data; // gets .country = ip location
lusercountry = usercountry.toLowerCase(); // ie. GB to gb
lusercountry = lusercountry.replace(/\s+/g, ''); // takes additional space off the string
country = "'" + lusercountry + "'"; // adds '' to the .country string
$("#country_selector").countrySelect("selectCountry", country);
});
$("#country_selector").countrySelect({
defaultCountry: 'gb',
//preferredCountries: []
});
答案 0 :(得分:1)
According to the documentation, the method countrySelect takes a string when used with "selectCountry". There is no need to quote the string like this:
country = "'"+lusercountry+"'";
Simply pass lusercountry
as is. i.e.
$("#country_selector").countrySelect("selectCountry", lusercountry);
答案 1 :(得分:-1)
just pass usercountry to countrySelect, and you can minify your cod like that
var usercountry = data.toLowerCase().trim();
$("#country_selector").countrySelect("selectCountry", usercountry);
Use trim() instead of replace, there is only one additional space at the end, trim do the job