0

あなたの座標を取得し、サーバーに送信して郵便番号と返品を取得するフォームがあります。

場所を取得をクリックすると、代わりにフォーム フィールドに結果を移動することを好むポップアップ フィールドが表示されます。

ジャバスクリプト

    function showLocation(position) {
 var latitude = position.coords.latitude;
 var longitude = position.coords.longitude;
  $.getJSON('http://www.uk-postcodes.com/latlng/' + position.coords.latitude + ',' + position.coords.longitude + '.json?callback=?', null, gotpostcode);
}

function errorHandler(err) {
  if(err.code == 1) {
    alert("Error: Access is denied!");
  }else if( err.code == 2) {
    alert("Error: Position is unavailable!");
  }
}


function gotpostcode(result)
{
  var postcode = result.postcode;
    $("#postcodegoeshere").val(postcode);
}

function getLocation(){

   if(navigator.geolocation){
      // timeout at 60000 milliseconds (60 seconds)
      var options = {timeout:60000};
      navigator.geolocation.getCurrentPosition(showLocation, 
                                               errorHandler,
                                               options);
   }else{
      alert("Sorry, browser does not support geolocation!");
   }
}

HTML

  <body>
<form>
     <input type="button" onclick="getLocation();"  
                             value="Get Location"/>

   </form>
   <div>
   <input id='postcodegoeshere' name='xxx' type='text' value='' />
</div>
</body> 
4

1 に答える 1

1

jQuery セレクターを使用する必要があります。.html(postcode)次に、入力 (チェックボックス、ラジオ、ボタンではありません) を使用して、または内部にそれを配置できます。.val(postcode)

function gotpostcode(result)
{
  var postcode = result.postcode;
  $('#postcodegoeshere').html(postcode);
}

セレクターに関する情報http://api.jquery.com/category/selectors/、このコードでは ID セレクターを使用しています。

要素の html を変更するhttp://api.jquery.com/html/

入力値の変更http://api.jquery.com/val/

于 2013-05-02T19:10:04.743 に答える