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 回答