Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript window.location.reload(true)重新加载页面,但页面需要刷新以显示更改_Javascript_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

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

Javascript window.location.reload(true)重新加载页面,但页面需要刷新以显示更改,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,有一个通过AJAX更改分类术语的函数。这非常有效,除了window.location.reload(true)上的内容保持不变之外,即使已经做了更改。请参阅下面的代码和GIF以了解 这是一个添加按钮并在单击时重新加载页面的示例 if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) { $text = (in_category( 'live') ? 'Activate' :

有一个通过AJAX更改分类术语的函数。这非常有效,除了
window.location.reload(true)
上的内容保持不变之外,即使已经做了更改。请参阅下面的代码和GIF以了解

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

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>';
}
JS

function-toggleLive(调用者,post\u-id)
{
var$=jQuery;
var$caller=$(调用者);
var waitText=“…”;
var liveText=“…”;
var deactivateText=“…”;
//检查是否没有正在进行的请求
如果($caller.text()==waitText){
返回false;
}
//获取要设置为的新值
var值=($caller.text()==liveText?1:0);
//更改文本以指示等待,而不更改按钮的宽度
$caller.width($caller.width()).text(waitText);
//Ajax请求
风险值数据={
动作:“切换现场”,
post:post_id,
价值:价值
};
jQuery.post(“/wp admin/admin ajax.php”),数据,函数(响应)
{
如果(响应=“1”){
//成功
如果(值){
$caller.text(停用文本);
}否则{
$caller.text(liveText);
}
}否则{
//错误
警报(“错误:+响应”);
//重置文本
如果(值){
$caller.text(停用文本);
}否则{
$caller.text(liveText);
}
}
//重置宽度
$caller.width(“自动”);
});
//防止链接单击发生
返回false;
}
它在非单数的页面上正常工作

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

尝试在Jquery中使用Live Query插件,而不是Live。

我可以通过设置
return trueOrFalse(bool)来实现这一点


我相信@cdoshi的回答是正确的,但我无法做到这一点。我确信进一步的探索会使这成为可能,但我的修复程序只需对我的代码做一点修改就达到了我想要的效果。

为什么不试试
document.location.reload()
?谢谢@NaveenKumarSangi。我已经试过了,结果是一样的。谢谢@cdoshi。我已经更新了这个问题,包括了JS和函数。你能建议我在哪里包括重新加载代码吗?感谢您的帮助。当您调用Jquery.post时,将在请求成功时执行回调函数。在回调函数中包含重新加载,并删除在单击时触发的重新加载。我不确定这是否是问题所在。我添加了
location.reload(true)到函数成功回调。没什么不同。还有其他建议吗?首先,确保从echo“
  • ”中删除重新加载;另外,确保回调是通过在回调中设置断点触发的是的,我试过了。我还向另一个页面添加了非单数的
    ,并按预期工作。GIF显示在更新的问题中。感谢您的建议。看起来很有趣,所以我会读一读。很高兴听到!!
    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');
    });
    
    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;
    }