逐步撤消窗体的历史记录

Step-wise undo history for a form

本文关键字:历史 记录 窗体 撤消      更新时间:2023-09-26

我正在寻找关于如何为表单实现逐步撤消的帮助/想法。我已经能够做到一个撤消事件,但我不知道如何更进一步。

在我的示例中,用户正在制作排序列表。他们选择自己的商品:

<input type="checkbox" id="food1">Bread<br>
<input type="checkbox" id="food2">Milk<br>
<input type="checkbox" id="food3">Juice<br>
<input type="checkbox" id="item1">Paper<br>
<input type="checkbox" id="item2">Pencils<br>
<input type="checkbox" id="item3">Eraser<br>

出现在框中:
<textarea id="text" rows="10" cols="25"></textarea>

该列表使用以下脚本生成:

// variable literals
foodtype = {
    food1: "Bread'n",
    food2: "Milk'n",
    food3: "Juice'n"
};
itemtype = {
    item1: "Paper'n",
    item2: "Pencils'n",
    item3: "Eraser'n"
};
// new text
var textbox = $('#text').val();
// old text to revert to
var oldtext = '';
// temporary holder for checkbox id
var choice = null;
// fill textarea with list
function filltext1() {
    if (choice !== null) {
        if (choice.indexOf('food') != -1) {
            textbox += foodtype[choice] + '';
            choice = null;
            $('#text').val(textbox);
        } else if (choice.indexOf('item') != -1) {
            textbox += itemtype[choice] + '';
            choice = null;
            $('#text').val(textbox);
        }
    }
}
// add new selection to list, unless unchecked
$('input:checkbox').change(function () {
    if ($(this).is(':checked')) {
        choice = $(this).attr('id');
        oldtext = textbox;
        filltext1();
    } else {
        $('#text').val(oldtext); // if unchecked, textarea = prior text
        textbox = oldtext; // reset variable to prior text
    }
});

最后一点是我的文本框,如果项目未选中,则返回一步。但是,只有当您取消选中最近的选择时。

异步取消复选框不会删除该项目的条目,而是删除最后一个选择。

如何将初始选择链接到可以恢复的状态?或者换言之,有没有一种方法可以异步撤消项目在列表中的条目?如果这就是全部可能的话,我甚至会对顺序撤消感到高兴。

我很感激你的帮助!

在折腾了一番之后,我采取了另一种方法。我没有试图撤消,而是删除了每个选中的项目。这允许在列表示例中进行异步删除。

这是一把最新的小提琴。

相关代码更改如下:

else {
        var del = $(this).attr('id'); // set unchecked id
        if (del.indexOf('food') != -1){
            oldtext = textbox.replace(foodtype[del], '');} // delete referenced literal
        else if (del.indexOf('item') != -1){
            oldtext = textbox.replace(itemtype[del], '');} // delete referenced literal            
        textbox = oldtext; // recapture old version
        $('#text').val(oldtext);
    }

我只需要重新构建列表。"撤消"功能在这里不合适,因为用户不一定会按照选中项目的顺序取消选中项目。当我选中Bread,选中Milk,然后取消选中Bread(Milk意外地从列表中删除(时,您当前的示例将失败。

// variable literals
var choiceMap = {
    food1: 'Bread',
    food2: 'Milk',
    food3: 'Juice',
    item1: 'Paper',
    item2: 'Pencils',
    item3: 'Eraser'
};
// Container for all the choices currently selected.
var choices = [];
// fill textarea with list
function filltext(choices) {
    var text = choices.join(''n');
    $('#text').val(text);
}
// add new selection to list, unless unchecked
$('input:checkbox').change(function () {
    var
        choiceKey = $(this).attr('id'),
        choice = choiceMap[choiceKey],
        index;
    if ($(this).is(':checked')) {
        choices.push(choice);
    } else {
        // ES5: index = choices.indexOf(choice);
        index = $.inArray(choice, choices);
        choices.splice(index, 1);
    }
    filltext(choices);
});

Fiddle。

或者,您可以在文本框中搜索要删除的项目,并用子字符串将其删除。但是,如果用户要编辑框的内容,你将无法找到字符串,或者你可能会找到错误的字符串(例如,如果你有项目"App"answers"Apple",对字符串进行索引将不起作用,你必须动态构建一个正则表达式(。

    <form>
    <input type="checkbox" id="food1" Bread="Bread" value="Bread"/>Bread<br />
    <input type="checkbox" id="food2" value="Milk"/>Milk<br />
    <input type="checkbox" id="food3" value="Juice"/>Juice<br />
    <input type="checkbox" id="item1" value="Paper"/>Paper<br />
    <input type="checkbox" id="item2" value="Pencils"/>Pencils<br />
    <input type="checkbox" id="item3" value="Eraser"/>Eraser<br />
    <textarea id="text" rows="10" cols="25"></textarea>
    </form>
    <script>
      var $check=$("input");
      var $text=$('textarea');
      $check.change(function(){
         $text.val($check.map(function(){
           if(this.checked){
           return this.value;
         }}).toArray().join(''n'));
      });
    </script>

您应该为复选框指定

http://embed.plnkr.co/f0YlZFGv1kE5lQurPqo5/preview