【问题标题】:How to force page refreshes or reloads in jQuery?如何在 jQuery 中强制页面刷新或重新加载?
【发布时间】:2011-02-17 17:17:28
【问题描述】:

当您输入地址并点击提交按钮时,下面的代码会显示 Google 地图和搜索结果。我一直在玩它来尝试 并在您点击提交按钮后强制页面完全刷新或重新加载。但我无法让它正常工作。它会在“页面中”加载结果,但我希望页面在结果加载时完全刷新,就像您点击浏览器上的后退按钮时一样。希望这是有道理的。

我认为答案就在这行代码中,但我对 jquery 不是很了解。它位于下面完整代码的底部附近。

<script type="text/javascript">
(function($) { 
    $(document).ready(function() {
        load();';

下面是完整的代码。任何帮助将不胜感激!

<?php
/*
SimpleMap Plugin
display-map.php: Displays the Google Map and search results
*/
$to_display = '';

if ($options['display_search'] == 'show') {
$to_display .= '
<div id="map_search" style="width: '.$options['map_width'].';">
    <a name="map_top"></a>
    <form onsubmit="searchLocations(\''.$categories.'\'); return false;"         name="searchForm" id="searchForm"     action="http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'">
        <input type="text" id="addressInput" name="addressInput" class="address"     />&nbsp;
        <select name="radiusSelect" id="radiusSelect">';

            $default_radius = $options['default_radius'];
            unset($selected_radius);
            $selected_radius[$default_radius] = ' selected="selected"';

            foreach ($search_radii as $value) {
                    $r = (int)$value;
                    $to_display .= '<option valu         e="'.$value.'"'.$selected_radius[$r].'>'.$value.' '.$options['units']."</option>\n";
            }

$to_display .= '    
        </select>&nbsp;
        <input type="submit" value="'.__('Search', 'SimpleMap').'"     id="addressSubmit" class="submit" />
        <p>'.__('Please enter an address or search term in the box above.',     'SimpleMap').'</p>
    </form>
</div>';
}
if ($options['powered_by'] == 'show') {
    $to_display .= '<div id="powered_by_simplemap">'.sprintf(__('Powered by %s     SimpleMap', 'SimpleMap'),'<a href="http://simplemap-plugin.com/"     target="_blank">').'</a></div>';
}

$to_display .= '
<div id="map" style="width: '.$options['map_width'].'; height:     '.$options['map_height'].';"></div>

<div id="results" style="width: '.$options['map_width'].';"></div>

<script type="text/javascript">
(function($) { 
    $(document).ready(function() {
        load();';    

        if ($options['autoload'] == 'some') {
            $to_display .= 'var autoLatLng = new GLatLng(default_lat,     default_lng);
            searchLocationsNear(autoLatLng, autoLatLng.lat() + ", " +     autoLatLng.lng(), "auto", "'.$options['lock_default_location'].'", "'.$categories.'");';
        }

        else if ($options['autoload'] == 'all') {
            $to_display .= 'var autoLatLng = new GLatLng(default_lat,     default_lng);
            searchLocationsNear(autoLatLng, autoLatLng.lat() + ", " +     autoLatLng.lng(), "auto_all", "'.$options['lock_default_location'].'",     "'.$categories.'");';
        }

$to_display .= '
    });
})(jQuery);
</script>';

?>

【问题讨论】:

    标签: javascript jquery google-maps refresh reload


    【解决方案1】:

    您不需要 jQuery 来执行此操作。拥抱 JavaScript 的力量。

    window.location.reload()
    

    【讨论】:

    • 按照语法,不就是:window.parent.window.location.reload()吗?
    • 我查看了 MozDevNet 发现,“父”窗口大多无法通过父窗口访问,您必须使用以下属性:developer.mozilla.org/en/DOM/window.openerwindow.opener.locaten.reload() 好吧window.parent 已经返回对父窗口的引用,因为 parent 是 window 类型。
    • 在我的例子中,window.location.reload() 破坏了我的代码,window.parent.location.reload() 使浏览器页面产生“频闪”效果。我正在尝试更新 Twitter 提要,当我更改主题(“屏幕名称”)时,我希望用新推文替换旧推文。
    • 使用 window.location.reload(true);因为如果它已经在缓存中,将使用缓存。
    【解决方案2】:

    将该行替换为:

    $("#someElement").click(function() {
        window.location.href = window.location.href;
    });
    

    或:

    $("#someElement").click(function() {
        window.location.reload();
    });
    

    【讨论】:

    • 嘿,谢谢大家。我对这些东西完全是个菜鸟,但是当我替换 load();用 window.location.reload();页面加载时页面会不断刷新,并且不会停止。您能告诉我在哪里进行更改吗?
    • onsubmit="searchLocations(\''.$categories.'\'); return false;"去掉“return false”。
    • 谢谢,但我仍处于无限刷新循环中。我做对了吗:
    • 那是因为每次页面加载时,只要 DOM 准备好,它就会再次刷新。基本上,您希望将其绑定到某个元素的 click 方法。请参阅更新的答案。如果该元素恰好是一个链接,return false 在事件处理程序的末尾。
    • @TimMac - 查看我的编辑,您需要将整个代码包含在准备好的文档中:$("#someElement").click(function() { window.location.href = window.location.href; });
    【解决方案3】:

    或者更好

    window.location.assign("relative or absolute address");
    

    这往往在所有浏览器和移动设备上都能发挥最佳效果

    【讨论】:

      【解决方案4】:

      替换为:

      $('#something').click(function() {
          location.reload();
      });
      

      【讨论】:

        【解决方案5】:

        您可以通过应用以下代码在添加新事件后刷新事件: -发布事件 -设置事件源 -重新渲染事件

          $('#calendar').fullCalendar('removeEvents');
                          $('#calendar').fullCalendar('addEventSource', YoureventSource);         
                          $('#calendar').fullCalendar('rerenderEvents' );
        

        这样就可以解决问题了

        【讨论】:

          猜你喜欢
          • 2017-06-20
          • 1970-01-01
          • 1970-01-01
          • 2016-02-11
          • 2013-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-29
          相关资源
          最近更新 更多