7

JavascriptとJQueryを使用してフォーム入力の値が変更されるたびに検出しようとしています。残念ながら、フォーカスを失っ$(elem).change()たときにしか変更イベントを発生させないため、JQuery では不十分だと思います。フォーム入力の値がいつ変更されたかelemすぐに知る必要があります。この目的のために、入力の値の可能な変更に関連するイベントを、keyuppastecutundo、およびredoに絞り込みました。ただし、javascriptもJQueryも、元に戻すまたはやり直しを処理する方法がないようです。

var onChange = function ()
{
    alert('Checking for changes...');
};

$(this).off('keyup').on('keyup', onChange);
$(this).off('paste').on('paste', onChange);
$(this).off('cut').on('cut', onChange);

$(this).off('undo').on('undo', onChange);  // undo ?
$(this).off('redo').on('redo', onChange);  // redo ?

Javascript/JQuery で元に戻す/やり直しイベントを探しましたが、何も役に立ちませんでした。元に戻す/やり直しイベントの処理方法について誰か助けてもらえますか?

4

6 に答える 6

7

JavaScript には取り消しまたはやり直しのイベントはありません。そのような機能が必要な場合は、JavaScript で自分で記述するか、そのような機能を提供するライブラリを見つける必要があります。

入力コントロールを変更できるすべての可能な方法をトラップして、そのような変更をすぐに確認できるようにする場合は、次のサンプル コードをご覧ください: http://jsfiddle.net/jfriend00/6qyS6/入力コントロールのコールバックを変更します。このコードはドロップダウン用に直接設計されたものではありませんが、入力コントロールの形式であるため、このコードを適応させてドロップダウン用の独自の変更イベントを作成することができます。

まあ、無限の知恵の StackOverflow は、jsFiddle への参照だけを投稿することを禁止しているため、ここにすべてのコードを貼り付ける必要があります (何らかの理由で、jsFiddle は他の Web 参照とは対照的に選択されています)。これを正確な解決策として表現しているわけではありませんが、入力コントロールに対するユーザーの変更を検出する方法に使用できるテンプレートとして:

(function($) {

    var isIE = false;
    // conditional compilation which tells us if this is IE
    /*@cc_on
    isIE = true;
    @*/

    // Events to monitor if 'input' event is not supported
    // The boolean value is whether we have to 
    // re-check after the event with a setTimeout()
    var events = [
        "keyup", false,
        "blur", false,
        "focus", false,
        "drop", true,
        "change", false,
        "input", false,
        "textInput", false,
        "paste", true,
        "cut", true,
        "copy", true,
        "contextmenu", true
    ];
    // Test if the input event is supported
    // It's too buggy in IE so we never rely on it in IE
    if (!isIE) {
        var el = document.createElement("input");
        var gotInput = ("oninput" in el);
        if  (!gotInput) {
            el.setAttribute("oninput", 'return;');
            gotInput = typeof el["oninput"] == 'function';
        }
        el = null;
        // if 'input' event is supported, then use a smaller
        // set of events
        if (gotInput) {
            events = [
                "input", false,
                "textInput", false
            ];
        }
    }

    $.fn.userChange = function(fn, data) {
        function checkNotify(e, delay) {
            // debugging code
            if ($("#logAll").prop("checked")) {
                log('checkNotify - ' + e.type);
            }

            var self = this;
            var this$ = $(this);

            if (this.value !== this$.data("priorValue")) {
                this$.data("priorValue", this.value);
                fn.call(this, e, data);
            } else if (delay) {
                // The actual data change happens after some events
                // so we queue a check for after.
                // We need a copy of e for setTimeout() because the real e
                // may be overwritten before the setTimeout() fires
                var eCopy = $.extend({}, e);
                setTimeout(function() {checkNotify.call(self, eCopy, false)}, 1);
            }
        }

        // hook up event handlers for each item in this jQuery object
        // and remember initial value
        this.each(function() {
            var this$ = $(this).data("priorValue", this.value);
            for (var i = 0; i < events.length; i+=2) {
                (function(i) {
                    this$.on(events[i], function(e) {
                        checkNotify.call(this, e, events[i+1]);
                    });
                })(i);
            }
        });
    }
})(jQuery);    

function log(x) {
    jQuery("#log").append("<div>" + x + "</div>");
}

// hook up our test engine    
$("#clear").click(function() {
    $("#log").html("");
});


$("#container input").userChange(function(e) {
    log("change - " + e.type + " (" + this.value + ")");
});
于 2013-03-20T05:51:55.657 に答える
2

John Resig (JQuery の作成者) による Hot Keys が役立つ場合があります

https://github.com/jeresig/jquery.hotkeys

readme ファイルから

複数の修飾子 (例: alt+ctrl+z) を使用する場合は、アルファベット順に定義する必要があります (例: alt+ctrl+shift)。

于 2013-03-20T05:50:17.770 に答える