navigator.geolocation.getCurrentPosition(成功); 函數(shù)成功(位置){ 讓 lat = 位置.座標(biāo).緯度; 令 lng = 位置.座標(biāo).經(jīng)度; 讓 coords = {"lat": lat, "lng": lng}; //帶有座標(biāo)的AJAX請(qǐng)求到geoNames以取得國(guó)家/地區(qū)代碼 $.ajax({ url: "libs/php/getCurrentCode.php", 類型:“帖子”, 資料類型:“json”, 數(shù)據(jù):座標(biāo), 成功:函數(shù)(結(jié)果){ console.log(JSON.stringify(結(jié)果)); if (result.status.name == "ok") { $(結(jié)果["數(shù)據(jù)"]["國(guó)家代碼"]); 讓 currCode = 結(jié)果.data; 控制臺(tái).log(currCode); //用檢索到的程式碼觸發(fā)更改 $('#countrySelect').val('currCode').change(); }; }; }); };</pre> <p>我的 php:</p>ini_set('display_errors', 'On'); 錯(cuò)誤報(bào)告(E_ALL); $executionStartTime = microtime(true); $url='http://api.geonames.org/countryCodeJSON?&lat=' 。 $_REQUEST['緯度'] 。 '&lng=' 。 $_REQUEST['lng'] 。 '&用戶名=&樣式=完整'; $ch=curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL,$url); $結(jié)果=curl_exec($ch); 捲曲_關(guān)閉($ch); $decode = json_decode($結(jié)果,true); $輸出['狀態(tài)']['代碼'] = '200'; $output['status']['name'] = 'ok'; $output['狀態(tài)']['描述'] = '成功'; $output['status']['returnedIn'] = intval((microtime(true) - $executionStartTime) * 1000) 。 ' 多發(fā)性硬化癥'; $output['data'] = $decode['countryCode']; //var_dump($解碼); header('內(nèi)容類型:application/json; charset=UTF-8'); echo json_encode($output);</pre> <p><br />></p>
$('#countrySelect').val('currCode').change();
這將把值設(shè)為字面字串currCode。您要將其設(shè)定為變數(shù)currCode中的值。為了做到這一點(diǎn),請(qǐng)刪除引號(hào):
$('#countrySelect').val(currCode).change();