Is toggleLive the function that makes the AJAX request? You are calling reload immediately on click before changes are reflected on the backend. If you are using Jquery include your reload code in the complete callback function that indicates completion of your AJAX request.
window.location.reload(true)重新加载页面,但页面需要刷新才能显示更改
Have a function that makes a change to taxonomy term via AJAX. This works great, except the content remains unchanged on window.location.reload(true)
even though the change has been made. See the code and GIF below to understand.
This is an example that adds the button and reloads page on click
if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) {
$text = (in_category( 'live') ? 'Activate' : 'Activate');
echo '<li><a href="" onclick="window.location.reload(true);return toggleLive(this, ' . $post->ID . ');">' . $text . '</a></li>';
}
So, is there another way that I can reload the page onClick
that may help? Also, the post modified date is not updating, yet changes have been made to the post.
Thanks in advance for your help
EDIT -
I have already tried
location.href = location.href;
and
document.location.reload();
ADDITIONAL INFO -
Function
add_action('wp_ajax_toggle_live', function(){
// Check ID is specified
if ( empty($_REQUEST['post']) ) {
die( __('No post ID specified.') );
}
// Load post
$post_id = (int) $_REQUEST['post'];
$post = get_post($post_id);
if (!$post) {
die( __('You attempted to edit an item that doesn&#8217;t exist. Perhaps it was deleted?') );
}
// Check permissions
$post_type_object = get_post_type_object($post->post_type);
if ( !current_user_can($post_type_object->cap->edit_post, $post_id) ) {
die( __('You are not allowed to edit this item.') );
}
// Load current categories
$terms = wp_get_post_terms($post_id, 'campaign_action', array('fields' => 'ids'));
// Add/remove Starred category
$live = get_term_by( 'live', 'campaign_action' );
$index = array_search($live, $terms);
if ($_REQUEST['value']) {
if ($index === false) {
$terms[] = $live;
}
} else {
if ($index !== false) {
unset($terms[$index]);
}
}
wp_set_object_terms( $post_id, 'live', 'campaign_action' );
die('1');
});
JS
function toggleLive(caller, post_id)
{
var $ = jQuery;
var $caller = $(caller);
var waitText = ". . .";
var liveText = ". . .";
var deactivateText = ". . .";
// Check there's no request in progress
if ($caller.text() == waitText) {
return false;
}
// Get the new value to set to
var value = ($caller.text() == liveText ? 1 : 0);
// Change the text to indicate waiting, without changing the width of the button
$caller.width($caller.width()).text(waitText);
// Ajax request
var data = {
action: "toggle_live",
post: post_id,
value: value
};
jQuery.post("<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php", data, function(response)
{
if (response == "1") {
// Success
if (value) {
$caller.text(deactivateText);
} else {
$caller.text(liveText);
}
} else {
// Error
alert("Error: " + response);
// Reset the text
if (value) {
$caller.text(deactivateText);
} else {
$caller.text(liveText);
}
}
// Reset the width
$caller.width("auto");
});
// Prevent the link click happening
return false;
}
- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答
3条回答
为你推荐
- 检查/设置页面加载时的按钮状态
- php
- html
- javascript
- jquery
- 1个回答
- Magento代码自适应在比较页面中显示自定义属性值
- magento
- php
- 1个回答
- 解析错误:语法错误,第150行的文件意外结束(PHP版本为新?)
- php
- themes
- 2个回答
- window.location.reload(true)重新加载页面,但页面需要刷新才能显示更改
- ajax
- php
- wordpress
- javascript
- jquery
- 3个回答
- 尝试使用php重新加载页面
- onclick
- refresh
- php
- javascript
- reload
- 1个回答
- 使用Javascript的PHP页面
- php
- html
- javascript
- jquery
- 4个回答
- 删除多页重新加载问题
- 5秒后javascript刷新页面
- php
- javascript
- 4个回答
- 不要使用Jquery-Ajax刷新菜单以刷新页面
- 如何在发送javascript表单后重定向页面
- php
- javascript
- jquery
- 3个回答
- 通过cURL发送数据而无需重新加载页面
- iframe
- ajax
- php
- javascript
- 2个回答
- 单击时使用ajax调用重新加载数据表数据
- datatables
- ajax
- php
- javascript
- jquery
- 1个回答
- PHP丢失会话的页面之间
- Ext.Window如何点击最大化窗口按钮后刷新窗口里面内容?
- ajax
- javascript
- 0个回答
- 表单数据提交完成后grid如何重新加载?
- javascript
- ext
- 0个回答
- 请问如何实现点击按钮刷新已打开页面的功能
- javascript
- ext
- 0个回答
- 这个问题的解决方案,反正项目能跑,不太要紧
- 解决方案
- 前端
- java
- 开发人员
- 1个回答
- jsp新人求助,登录页面设定验证码和密码确认之后,在表单内提交前校验完全没响应
- 表单
- java
- javascript
- 2个回答
- mvc artdialog 弹出页面为空,不显示内容?
- mvc
- artdialog
- 3个回答
- artdialog 弹出页面是空的,不显示指定页面的内容
- mvc
- artdialog
- 3个回答