Javascript 如何在 jQuery 中强制页面刷新或重新加载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2803655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force page refreshes or reloads in jQuery?
提问by TimMac
The code below displays a Google map and search results when you enter an address and hit the submit button. I've been playing with it to try and force the page to completely refresh or reload once you hit the submit button. But I can't get it to work right. It loads the results "in page," but I'd like the page to completely refresh when the results load, like when you hit the back button on your browser. Hope that makes sense.
当您输入地址并点击提交按钮时,下面的代码会显示 Google 地图和搜索结果。我一直在尝试使用它来尝试在您点击提交按钮后强制页面完全刷新或重新加载。但我无法让它正常工作。它在“页面中”加载结果,但我希望页面在结果加载时完全刷新,例如当您点击浏览器上的后退按钮时。希望这是有道理的。
I think the answer lies in this line of code but I don't know jquery very well. It's near the bottom of the full code below.
我认为答案就在这行代码中,但我不太了解 jquery。它靠近下面完整代码的底部。
<script type="text/javascript">
(function($) {
$(document).ready(function() {
load();';
Here's the full code below. Any help would be greatly appreciated!
这是下面的完整代码。任何帮助将不胜感激!
<?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" />
<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>
<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>';
?>
回答by Diodeus - James MacFarlane
You don't need jQuery to do this. Embrace the power of JavaScript.
您不需要 jQuery 来执行此操作。拥抱 JavaScript 的强大功能。
window.location.reload()
回答by karim79
Replace that line with:
将该行替换为:
$("#someElement").click(function() {
window.location.href = window.location.href;
});
or:
或者:
$("#someElement").click(function() {
window.location.reload();
});
回答by WiR3D
Or better
或更好
window.location.assign("relative or absolute address");
that tends to work best across all browsers and mobile
这往往在所有浏览器和移动设备上效果最佳
回答by user1016195
Replace with:
用。。。来代替:
$('#something').click(function() {
location.reload();
});
回答by Jabulani Vilakazi
You can refresh the events after adding new ones by applying the following code: -Release the Events -set Event Source -Re-render Events
您可以通过应用以下代码在添加新事件后刷新事件: - 释放事件 - 设置事件源 - 重新渲染事件
$('#calendar').fullCalendar('removeEvents');
$('#calendar').fullCalendar('addEventSource', YoureventSource);
$('#calendar').fullCalendar('rerenderEvents' );
That will solve the problem
这将解决问题

