如何在标签值发生变化时使用jquery事件?

时间:2013-12-18 01:11:35

标签: javascript jquery

大家好,这里需要一些帮助。 我正在创建一个动态表单,我在使用jquery获取HTML的标签值时遇到了麻烦。我有一个表格,里面是谷歌地图。每当Google地图更新当前的纬度和经度时,其当前纬度和经度的标签也会发生变化。我需要做的是获取当前的纬度和经度并将其显示在文本框中。更改的文本框值也必须是动态的。

在我的代码中,我有这个。

$(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事件,但我不知道。

2 个答案:

答案 0 :(得分: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 :(得分: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());
});

我希望我已经明白你想要实现的目标。