2

ノックアウトで条件付きぼかしの入力値を更新しようとしています - 基本的に、特定の要素がぼかしをトリガーしたときに、要素が値の更新をトリガーしないようにします。ドキュメントのすべての要素を見て、最後にクリックされたものを判断できることはわかってmousedownいますが、少しやりすぎのようです。誰でも考えられる他の回避策はありますか?

<input class="edit" data-bind="value: title, valueUpdate: 'afterkeydown', enterKey:     $root.stopEditing, selected: editing, event: { blur: $root.checkEditing }">

これをやってのけるために私が達成しようとしていたコードは、document.activeElement.

self.checkEditing = function( item, event ) {
        if (document.activeElement == $('a.cancel')) {

            // revert to previous title, aka cancel the editing
            item.title(item.previousTitle);
            item.editing( false );

        } else {
                            // this will update value with whatever was typed right before the blur
            item.editing( false );

            if ( !item.title().trim() ) {
                self.remove( item );
            }
        }
    };
4

1 に答える 1

1

ぼかしをトリガーした要素を適切にキャプチャするように見えますが、setTimeout を使用する必要があります。ぼかしが処理された後、setTimeout は、フォーカスされた要素が使用可能になったことを確認します。

例えば:

入力:

<input class="edit" data-bind="value: title, valueUpdate: 'afterkeydown', selected: editing, event: { blur: $root.checkEditing, click: $root.editItem }">

ぼかした後にアクティブな要素をチェックするメソッド:

   self.checkEditing = function( item, event ) {

        setTimeout(function(){

            console.log("The active element is: " + document.activeElement)

            // check if the user selected cancel
            if ($(document.activeElement).is("a.cancel")) {

                // revert to previous title
                item.title(item.previousTitle);
            }
        });

        item.editing( false );

        if ( !item.title().trim() ) {
            self.remove( item );
        }

    };

これを示す完全なフィドルはこちらです:http://jsfiddle.net/hUA9v/5/

于 2013-02-25T02:44:01.737 に答える