2013-12-18 26 views
0

大家好,這裏需要一點幫助。 我正在創建一個動態表單,並且在使用jquery獲取HTML標籤值方面遇到了一些麻煩。我擁有的是一張表格,裏面是Google Map。每次Google地圖更新時,它都是當前的經緯度,它當前的經緯度標籤也在變化。我需要做的是獲取當前的經緯度並將其顯示在文本框中。改變的文本框的值也必須是動態的。如何在標籤值發生變化時使用jquery事件?

在我的代碼中,我有這個。

$(document).ready(function(){ 

    var current_latitude = $('#company_geo_lat').val(); //this is the textbox 
    var current_longitude = $('#company_geo_long').val(); //this is the textbox 

    var new_latitude = $('.new_latitude').html(); //this is the label 
    var new_longitude = $('.new_longitude').html(); //this si the label 

}); 

我應該怎麼做才能動態替換文本框的值?我不想使用onchange事件,但我不知道。

回答

1

你可以使用http://api.jquery.com/change/

例如:

$('#company_geo_lat').change(function() { 
    $('.new_latitude').html($(this).val()); 
}); 

$('#company_geo_long').change(function() { 
    $('.new_longitude').html($(this).val()); 
}); 

除了JQuery的,你可以考慮AngularJS http://angularjs.org。在angularjs的首頁有一些有用的例子。

1

我想對你最好的是添加事件

google.maps.event.addListener(map, "center_changed", function() { // map is your map var 
    var center = map.getCenter(); 
    $('.new_latitude').html(center.lat()); 
    $('.new_longitude').html(center.lng()); 
}); 

我希望我已經明白了你想要的目的。

+0

好的,我會嘗試。 :) – Jerielle

相關問題