首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >window.location.reload(true)重新加载页面,但页面需要刷新以显示更改

window.location.reload(true)重新加载页面,但页面需要刷新以显示更改
EN

Stack Overflow用户
提问于 2017-12-09 04:39:41
回答 3查看 580关注 0票数 0

具有一个通过AJAX对分类法术语进行更改的函数。这是很好的工作,只是内容在window.location.reload(true)上保持不变,即使已经做了更改。请参阅下面的代码和GIF以了解。

这是一个在单击时添加按钮并重新加载页面的示例。

代码语言:javascript
复制
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>';
}

那么,还有其他方法可以帮助我重新加载页面onClick吗?此外,员额修改日期尚未更新,但已对该员额进行了更改。

提前感谢您的帮助

编辑-我已经尝试过location.href = location.href;document.location.reload();

更多信息-

函数

代码语言:javascript
复制
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&amp;#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

代码语言:javascript
复制
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;
}

它就在不奇异的页面上工作

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-12-09 04:57:49

toggleLive是发出AJAX请求的函数吗?在后端反映更改之前,您将立即在单击时调用reload。如果使用Jquery,则在指示AJAX请求完成的完整回调函数中包含重新加载代码。

票数 1
EN

Stack Overflow用户

发布于 2017-12-09 06:03:45

尝试使用jquery中的Live插件而不是活动。

票数 0
EN

Stack Overflow用户

发布于 2017-12-09 08:07:51

我能够通过在JS中设置return trueOrFalse(bool);并将页面的permalink添加到函数中的<a href="">来实现这一点。

我相信@cdoshi在他们的回答是正确的,但我无法做到这一点。我确信,进一步的探索将使这成为可能,但我的修复实现了我想要的,而我的代码几乎没有改变。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47725368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档