Google Maps API v3,在动态调整大小的信息窗口中验证html表单

问题描述:

我在使用jQuery验证插件的Google地图信息窗口中获得了一个HTML表单。当它显示错误消息时(我已经将它们移到了表单输入下),表单中的div溢出了信息窗口。Google Maps API v3,在动态调整大小的信息窗口中验证html表单

我读过类似问题的回复,但我没有得到任何解决方案的解决方案。

HTML形式:

<div id="formDiv"> 
    <form action="addRow.php" method="post"id="htmlForm"> 
    <fieldset> 
     <p id="contactP"> 
      <label for="contact">Email:</label> 
      <input type="text" name="contact" id="contact"/> <br/> 
     </p> 
     <p id="dateP"> 
      <label for="datepicker">Date:</label> 
      <input type="text" name="date" id="datepicker"/> <br/> 
     </p> 
     <p id="latP"> 
      <label for="lat">Latitude:</label> 
      <input type="text" name="lat" id="lat" class="location"/> <br/>'+ 
     </p> 
     <p id="lngP"> 
      <label for="lng">Longitude:</label> 
      <input type="text" name="lng" id="lng" class="location"/> <br/> 
     </p> 
     <p id="descP"> 
      <label for="desc" id="dos">Description of Sighting:</label> <br/> 
      <textarea name="desc" id="desc"></textarea><br/> 
     </p> 

      <input type="submit" class="button" id="submitBtn" value="Post Sighting!" onclick="postSighting();"/>'+ 
     <p class="clear"></p>'+ 
    </fieldset></form></div> 

;

的jQuery:

$('#htmlForm').validate({ 
    rules: { 
     contact: {required:true, email:true}, 
     date: {required:true}, 
     lat: {required:true}, 
     lng: {required:true}, 
     desc: {required:true}, 
    }, 
    messages: { 
     desc: 'Please be descriptive!' 
    }, 
    errorPlacement: function(error, element) { 
     error.appendTo($('#' + element.attr('id') + 'P')); 
     if (element.attr('id') == 'desc') { 
      error.css({ 
       text_align: 'center', 
       color: 'red', 
       padding: 45, 
      }); 
     } else { 
      error.css({ 
       color: 'red', 
       padding: 10, 
       margin:0, 
      })}; 
    } 
}); 
+0

为什么标记为google-maps-api-3? – andresf 2012-03-15 16:57:38

+0

这就是整个问题 - 它是Google地图信息窗口中的HTML表单。信息窗口不会重新调整大小以容纳包含插入的错误消息的div。 – Roy 2012-03-15 17:03:43

+0

链接到实时代码将是非常有价值的。您的代码与Maps API没有任何关系。除非您提供代码链接并提供更多详细信息,否则您可能无法获得答案。 – andresf 2012-03-15 17:06:54

第一:你必须确保新的内容将永远融入信息窗口(信息窗口不能比的地图大)

问题:当验证 - 插件修改内容,infoWindow的content_changed事件不会触发(但这是调整infoWindow的大小所必需的)

触发infoWindow的content_changed事件将不足以重置内容到cu infoWindow的rrent content-property(当你的插件修改内容时它没有改变)

所以你必须在插件修改标记之后设置内容,方法是将内容设置为infoWindow中的当前子树。听起来很复杂,但不是:

infowindowObject.setContent(document.getElementById('formDiv')); 

这不会有DOM的任何影响,但它会:

  1. 设置信息窗口的内容属性的当前内容
  2. 触发content_changed事件,因此应调整infoWindow的大小。
+0

感谢您的回应;在validate插件完成后,我重置了infoWindow的内容,但infowindow仍然没有重新调整大小。我在控制台中收到以下错误: Uncaught TypeError:无法调用未定义的方法'setContent' 但是infoWindow变量是全局的。 – Roy 2012-03-16 11:27:46

+0

当我遵循你的小提琴http://jsfiddle.net/rhewitt/gqa6k/1/时,有一个全局的markerWindow变量,但是这个变量将不包含infoWindow,因为你在placeMarker()中通过使用var-关键字,在函数内部创建一个新的markerWindow变量,它不是全局的。删除var-keyword。 – 2012-03-16 11:42:41

+0

我没有意识到我重新宣布变量谢谢 - 错误纠正。 infowindow现在部分重新调整大小!我要玩CSS值,看看是否照顾它。 – Roy 2012-03-16 12:28:36