开发者

JQuery: Refresh Page as Part of a Function

FOLLOW UP:

The solutions below work great for refreshing the page. Thanks!

The project I have allows users to click a back arrow and view past versions of the edited text.

What I really want to do is always direct the user back to the index page (project.php) after editing any page (they can seemingly edit past versions (ex. project.php/version5), but recent edits are only displayed on the index page). This works great when I use:

function update(id){
// get value of user input field
var value = $("#newtext").val();

// save new word to database
$.post("save.php?word_id=" + id + "&word=" + value, function(data){

// redirects edits of past versions of text to current version on index page
window.location = "project.php";

});

}

This always brings the user back to the main page if he edits in the "past" (say on projectphp/version5).

However, for some reason editing on the index page itself now does not actually cause the page to refresh itself (as it did when I used window.location.reload) or display new edits unless the user manually reloads the page.

Is this a cache issue, and can I resolve it?


Hello. I am using an inline editor jQuery plugin and I've written a function that updates the user's text edits to a mySQL database when the save button is clicked.

I would also like the page to refresh when that button is clicked.

I'm wondering if I can simply add a refresh line to the function or if I must write a new function (and what that would be). I've been through the related StackOverflow questions and none of the jQuery refresh lines I've used seem to be doing the trick. For example this does not work:

function update(id){

// get value of user input field
var value = $("#newtext").val();


// save new word to database
$.get("save.php?word_id=" + id + "&word=" + value);

location.reload();

}

The function is used later in the code as such:

.html('<input type="text" maxlength="36" id="newtext" value="'+ self.value() +'"> <button onClick="update(' + id + ');">' 开发者_开发百科+ options.buttonText +'</button>')

This is probably a fairly easy fix, but I'm a bit new to the game. Any words of advice would be appreciated!


You can just do:

window.location.href = window.location.href;

But you should do it in the callback function for the .get() to make sure that's completed first:

$.get("save.php?word_id=" + id + "&word=" + value, function(data){
  window.location.href = window.location.href;
});


You could also use window.location.reload()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜