开发者

Revert values back when changing tabs in jQuery

开发者 https://www.devze.com 2022-12-16 13:02 出处:网络
I use jQueryUI\'s tabs and when a user changes a form in the tab, it promprts the user tha开发者_JS百科t a change has been made.

I use jQueryUI's tabs and when a user changes a form in the tab, it promprts the user tha开发者_JS百科t a change has been made.

My question is, how do I revert the values before being changed when the user presses 'OK' on my confirm() prompt?


You'll have to keep track of what the values were before changing them... so something like this should work for you:

var oldValues = {};
$(function() {
    $(":input").each(function() {
        oldValues[$(this).attr("id")] = $(this).val();
    });
});
function revertValues() {
    for (var oldVal in oldValues) {
        $("#" + oldVal).val(oldValues[oldVal]);
    }
}

And then just call revertValues when you hit OK in your confirmation dialog.

A few notes:

  • I just wrote this code straight into the browser, so it's completely untested...
  • Assuming you have other inputs on other tabs, you'll need to change the :input selector and keep different sets of oldValues for each tab (& then obviously only revert the values for the current tab.
0

精彩评论

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

关注公众号