如何从GMaps infowindow触发jQuery [英] How to trigger jQuery from GMaps infowindow

查看:104
本文介绍了如何从GMaps infowindow触发jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从GMap信息窗口触发jQuery。我有这些代码为我的GMap:

  var theContent =< form action ='#'method ='post'onSubmit ='return false;'>< input type ='text'name ='firstname'/>; 
theContent + =< input type ='submit'value ='Save'/>< / form>;

var infowindow = new google.maps.InfoWindow({
content:theContent
});
infowindow.open(map,marker);

以及这些jQuery代码:

 < script src =http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js>< / script> 
< script type ='text / javascript'>
$(document).ready(function(){
$('form')。bind('submit',function(){
var str = $('form')。 serialize();

$ .ajax({
type:POST,
url:save.php,
data:str,
成功:function(msg){
alert('Saved!');
}
});

返回false; //让页面赢得' t refresh
});
});
< / script>

问题是,我对infowindow文本框的输入没有保存,这就是为什么我认为jQuery isn 't触发内部GMap infowindow / bubble。



当我在GMap之外尝试表单时,它可以正常工作。

感谢您的帮助!

解决方案

现在,jQuery代码绑定到文档上的表单,但不一定信息窗口打开。因此,当infowindow甚至不是DOM的一部分时,就可能绑定到表单,这是不可能的。您需要:


  1. 调用 $('form')。bind(...) RIGHT AFTER infowindow.open(...) OR

  2. 更改 $('form ').bind(...) $('form')。live(...)。这将针对页面上的所有现在和未来表单。


I want to trigger jQuery from a GMap info window. I have these code for my GMap:

var theContent = "<form action='#' method='post' onSubmit='return false;'><input type='text' name='firstname' />";
theContent += "<input type='submit' value='Save' /></form>";

var infowindow = new google.maps.InfoWindow({       
    content: theContent
});
infowindow.open( map, marker);

and these code for jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type='text/javascript'>
    $(document).ready(function() {
        $('form').bind('submit',function() {
            var str = $('form').serialize();

            $.ajax({
                type: "POST",
                url: "save.php",
                data: str,
                success: function(msg){
                    alert('Saved!');
                }
            });

            return false; //so the page won't refresh
        });
    });
</script>

The problem is, my input to the infowindow textbox isn't saving, that's why I think jQuery isn't triggered inside GMap infowindow/bubble.

When I tried the form outside GMap, it works fine.

Thanks for any help!

解决方案

Right now, the jQuery code is binding to the form on document ready, but not necessarily when the info window is open. Therefore, it's possible you are binding to the form when the infowindow is not even part of the DOM, which is not possible. You need to either:

  1. Call $('form').bind(...) RIGHT AFTER infowindow.open(...) OR
  2. Change $('form').bind(...) to $('form').live(...). This will target all present and future forms on the page.

这篇关于如何从GMaps infowindow触发jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆