[英]How can I refresh the append data without page refresh jquery?
I am trying to use ++ in jquery to append data, and I face a problem, I need to refresh the value again if I click other button without refresh page, how can I do that? 我正在尝试在jQuery中使用++追加数据,但遇到一个问题,如果我单击没有刷新页面的其他按钮,则需要再次刷新值,该怎么办? The var count will increase as I clicked, I want to know if I can start over this count again when I click second button. 单击后,无功计数将增加,我想知道当我单击第二个按钮时是否可以再次从该计数重新开始。
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++ });
Updated Answer (based on clarification from OP) : 更新的答案 (基于OP的说明) :
I want to if I can start over this count again when I click second button 我想单击第二个按钮时是否可以重新开始计算
$('#second').on('click',function(){
//refresh the var count, start from 1 again
count = 1;
});
Live Example: 现场示例:
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>
Or if you wanted the first incremented number to be 1, start with count = 0
: 或者,如果您希望第一个递增的数字为1,则从count = 0
开始:
Live Example: 现场示例:
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>
Original Answer : 原始答案 :
I'm going to take a total guess at it here and say that in your ajax
callback, you're modifying the page in some way, for instance: 我将在这里对此进行全面猜测,并说在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++
}
});
});
And that you want a click on the other element to reset things back to the way they were when the page was first loaded. 并且您想要单击其他元素以将内容重置为首次加载页面时的状态。
If so , then you can do something like this (see comments): 如果是这样 ,那么您可以执行以下操作(请参阅评论):
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;
}
});
Obviously, many aspects of the above would vary based on what you're actually trying to do, but as you haven't told us what that is, this is the best I can do... 显然,以上内容的许多方面会根据您实际要执行的操作而有所不同,但是由于您没有告诉我们这是什么,这是我能做的最好的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.