2011-04-08 46 views
0

用戶這種形式的上進入地址,然後在驗證地址,並運行此:訪問AJAX/jquery的返回的數據,並將其放置到形成場

$('#verify').click(function() { 

    var address = $('input[name=address]'); 
    var city = $('input[name=city]'); 
    var state = $('input[name=state]'); 
    var zip  = $('input[name=zip]'); 
    var country = $('select[name=country]'); 

    //organize the data for the call 
    var data = 'address=' + address.val() + '&city=' + city.val() + '&state=' + state.val() + '&zip=' + zip.val() + '&country=' + country.val(); 

    //start the ajax 
    $.ajax({ 
     url: "process.php", 
     type: "GET", 
     data: data, 
     cache: false, 
     success: function (html) { 

      //alert (html); 

      if (html!='error') { 

       //show the pin long and lat form 
       $('.form2').fadeIn('slow'); 

      } else alert('Error: Your location cannot be found'); 
     } 
    }); 

    //cancel the submit button default behaviours 
    return false; 
}); 

process.php需要的數據,並驗證地址是否存在,然後將緯度和經度發回爲「經度,緯度」。如何獲取這些數據並將其放置在表單字段中,該表單字段顯示該位置是否已找到(form2 div中的表單字段)。非常感謝。

回答

1

替換此:

$('.form2').fadeIn('slow'); 

與此:

$('.form2').fadeIn('slow').find('input[name=latlon]').val(html); 

其中latlon是要插入的緯度和經度輸入字段的名稱。

+0

如果它們是2個需要更新的字段呢?一個是緯度,一個是經度?再次感謝! – Sergio 2011-04-08 23:51:32

1

那麼,我很亂,但如果我理解正確,你可以通過「,」分隔符分割響應,並簡單地把每個分別放在相應的字段中。像這樣:

if (html!='error') { 
    var response = html.split(','); 

    $('.form2').fadeIn('slow'); 
    $('.form2 input.longitude').attr('value', response[0]); 
    $('.form2 input.latitude').attr('value', response[1]); 
} else { 
    alert('Error: Your location cannot be found'); 
} 
+0

布魯諾,這正是我想要做的......但由於某些原因,這些值並沒有顯示在字段上。我可能會做錯事。 Sergio 2011-04-08 23:43:18

+1

如果你想使用布魯諾的代碼,那麼用輸入#經度和輸入緯度替換input.longitude,輸入#緯度 - 它也可能值得閱讀 - > http://api.jquery.com/category/selectors/ – rATRIJS 2011-04-08 23:50:35

+0

rATRIJS,非常感謝。作品!現在將讀取您發送的鏈接。再次感謝。 – Sergio 2011-04-08 23:57:50

相關問題