【问题标题】:Google Maps API v3, validation of html form in info-window with dynamic resizeGoogle Maps API v3,在信息窗口中使用动态调整大小验证 html 表单
【发布时间】:2012-03-15 16:09:47
【问题描述】:

我在使用 jQuery 验证插件的 Google 地图信息窗口中有一个 HTML 表单。当它显示错误消息(我已将它们移动到表单输入下)时,带有表单的 div 会溢出信息窗口。

我已经阅读了对类似问题的回复,但我还没有解决任何这些解决方案。

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,
            })};
    }
});

【问题讨论】:

  • 为什么这个标签是 google-maps-api-3?
  • 这就是重点——它是谷歌地图信息窗口内的 HTML 表单。信息窗口不会重新调整大小以适应包含插入的错误消息的 div。
  • 实时代码的链接将非常有价值。您的所有代码都与 Maps API 无关。除非您提供代码链接并提供更多详细信息,否则您可能不会得到答案。
  • JsFiddle 似乎没有运行验证代码——该部分在我端正确运行(ie9 & chrome)。 jsfiddle.net/rhewitt/gqa6k/1

标签: css jquery google-maps-api-3


【解决方案1】:

首先:您必须确保新内容始终适合 infoWindow(infoWindow 不能大于地图)

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

仅触发 infoWindow 的 content_changed-event 是不够的,这会将内容重置为 infoWindow 的当前 content-property(在您的插件修改内容时不会更改)

所以你必须在插件修改标记后通过将内容设置为 infoWindow 内的当前子树来设置内容。听起来很复杂,其实不然:

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

这不会对 DOM 产生任何影响,但会:

  1. 将 infoWindow 的 content-property 设置为当前内容
  2. 触发 content_changed 事件,因此应该调整 infoWindow 的大小。

【讨论】:

  • 感谢您的回复;验证插件完成后,我重置了 infoWindow 的内容,但 infowindow 仍然没有重新调整大小。我在控制台中收到以下错误: Uncaught TypeError: Cannot call method 'setContent' of undefined 但是 infoWindow 变量是全局的。
  • 当我跟着你的小提琴jsfiddle.net/rhewitt/gqa6k/1 有一个全局markerWindow-variable,但这个变量不会包含infoWindow,因为你使用var-keyword在placeMarker()中创建markerWindow,创建了什么函数内部的一个新的标记窗口变量,它不是全局的。删除 var 关键字。
  • 我没有意识到我重新声明了该变量,谢谢——错误已纠正。信息窗口现在部分重新调整大小!我将使用 CSS 值,看看是否能解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多