繁体   English   中英

如何在没有页面刷新jQuery的情况下刷新附加数据?

[英]How can I refresh the append data without page refresh jquery?

我正在尝试在jQuery中使用++追加数据,但遇到一个问题,如果我单击没有刷新页面的其他按钮,则需要再次刷新值,该怎么办? 单击后,无功计数将增加,我想知道当我单击第二个按钮时是否可以再次从该计数重新开始。

 var count='1'; $('#good').on('click',function(){ $.ajax({ url: MyAjaxSearch.ajaxurl, type:'POST', cache: false, data:data, success: function(data){ count++ } }); }): $('#second').on('click',function(){ //refresh the var count, start from 1 again count++ }); 

更新的答案 (基于OP的说明)

我想单击第二个按钮时是否可以重新开始计算

$('#second').on('click',function(){
    //refresh the var count, start from 1 again
    count = 1;
});

现场示例:

 var count = 1; $('#good').on('click', function() { count++; snippet.log("Incremented, count = " + count); }); $('#second').on('click', function() { //refresh the var count, start from 1 again count = 1; snippet.log("Reset, count = " + count); }); 
 <input type="button" id="good" value="Increment"> <input type="button" id="second" value="Reset"> <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

或者,如果您希望第一个递增的数字为1,则从count = 0开始:

现场示例:

 var count = 0; $('#good').on('click', function() { count++; snippet.log("Incremented, count = " + count); }); $('#second').on('click', function() { //refresh the var count, start from 0 again count = 0; snippet.log("Reset, count = " + count); }); 
 <input type="button" id="good" value="Increment"> <input type="button" id="second" value="Reset"> <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 


原始答案

我将在这里对此进行全面猜测,并说在ajax回调中,您正在以某种方式修改页面,例如:

$('#good').on('click', function() {
    $.ajax({
        url: MyAjaxSearch.ajaxurl,
        type: 'POST',
        cache: false,
        data: data,
        success: function(data) {
            $("selector-for-some-elements").html("new content"); // <===
            current_page++
        }
    });
});

并且您想要单击其他元素以将内容重置为首次加载页面时的状态。

如果是这样 ,那么您可以执行以下操作(请参阅评论):

var current_page = '1';
// *** A variable for remembering the original content
var originalContent;

$('#good').on('click', function() {
    $.ajax({
        url: MyAjaxSearch.ajaxurl,
        type: 'POST',
        cache: false,
        data: data,
        success: function(data) {
            // Grab the elements we're going to change
            var elementsToChange = $("selector-for-elements");

            // If we don't have saved content...
            if (!originalContent) {
                // Save the original content
                originalContent = elementsToChange.clone();
            }

            // Modify it
            elementsToChange.html(/*....*/);

            current_page++
        }
    });
});

$('#second').on('click', function() {
    // If we have original content...
    if (originalContent) {
        // Put it back
        $("selector-for-some-elements").replaceWith(originalContent);
        current_page = 1;
    }
});

显然,以上内容的许多方面会根据您实际要执行的操作而有所不同,但是由于您没有告诉我们这是什么,这是我能做的最好的...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM