0

入力の長さに応じてフォーム送信ボタンを無効にする基本機能を実行します。

私はこれを使用しています:

    // check if the newpost text area in empty on page load and also input change
$(document).ready(function(){
    disableNewPost();
    $('#newpost').keyup(disableNewPost);
});

// disable new post submit button if the new post ckeditor is empty
function disableNewPost() {
    if ($('#newpost').val().length > 0) {
        $(".addnewpostbtn").prop("disabled", false);
    } else {
        $(".addnewpostbtn").prop("disabled", true);
    }
}

これは、通常のテキスト領域で機能します: http://jsfiddle.net/QA22X/

しかし、CKEditor を使用する場合は機能しません。

私は自分の研究をしましたが、うまくいくものを見つけることができません。

私はこれを見て試しました:

    // check if the newpost text area in empty on page load and also input change
$(document).ready(function(){
    disableNewPost();
    var e = CKEditor.instances['#newpost']
    e.on( 'keyup', function( event ) {
    disableNewPost();
    });
});

// disable new post submit button if the new post ckeditor is empty
function disableNewPost() {
    var value = CKEDITOR.instances['#newpost'].getData();
    if ($(value).length > 0) {
        $(".addnewpostbtn").prop("disabled", false);
    } else {
        $(".addnewpostbtn").prop("disabled", true);
    }
}

しかし、それもうまくいきません。

これについて何か助けはありますか?

4

2 に答える 2

1

前述のように、このようなエディタは空の要素を持つか、ラッパー要素内のコンテンツを返す傾向があるため、値が空であるように見えても、決して空になることはありません。この場合、これは役に立ちます...

// disable new post submit button if the new post ckeditor is empty
function disableNewPost() {
    var value = CKEDITOR.instances['#newpost'].getData();
    if ($(value).text() == "") {
        $(".addnewpostbtn").prop("disabled", false);
    } else {
        $(".addnewpostbtn").prop("disabled", true);
    }
}
于 2014-02-12T21:10:41.090 に答える
0

アーチャーの答えはうまくいくかもしれませんが、ここに別のもっともらしい解決策があると思います

  1. エディターの準備が整い、コンテンツがロードされたら、実行しますeditor.resetDirty()(instanceready、setData コールバック、または任意のもの)。
  2. を作成し、setInterval()その中でエディターが汚れているかどうかを確認しますeditor.checkDirty()
  3. 汚れていないときは、$(".addnewpostbtn").prop("disabled", false);必要なことをしてください
  4. それがあれば、$(".addnewpostbtn").prop("disabled", true);あなたが必要とすることは何でもします

フラグや jquery キャッシュなどを使用してパフォーマンスをかなり調整できますが、基本的な考え方は、CKEditor API を使用してダーティ (コンテンツが変更された) かどうかを確認することです。また、他のフォーム要素がダーティであるかのように、おそらくダーティチェック用の他の変数もありますが、それらを理解することはできます。

于 2014-02-19T07:54:03.627 に答える