簡體   English   中英

使用此元素而不是標識符獲取textarea的值

[英]Get value of textarea with this element and not identifier

我想在tinymce中檢查空文本區域

$(".tooltip-empty-editor").each(function() {        

    if( $.trim( tinymce.get(this).getContent() ) == '' ) { 
        // .....
    }

});

HTML

<textarea class="editor tooltip-empty-editor " name="modif" >....</textarea>

但是,這我得到錯誤tinymce.get(this).getContent()this ,他期待一個ID。

我如何使用它with tinymce來獲得textarea的價值?

為什么不能定義id並按如下方式訪問它?

$(".tooltip-empty-editor").each(function() {        

    if( $.trim( tinymce.get($(this).attr("id")).getContent() ) == '' ) { 
        // .....
    }

});

根據textarea與彼此之間的關系,您可以嘗試查找索引。 你需要一個sibling ,或者如果他們都是同一個節點( parent )的孩子,不需要打擾=> $(this).index()

如果頁面上只有一個=> textarea = tinymce.get(0)

HTML期望:

<div class="parent">
    <div class="sibling">
        <textarea class="editor tooltip-empty-editor " name="modif" >....</textarea>
    </div>
    <div class="sibling">
        <textarea class="editor tooltip-empty-editor " name="modif" >....</textarea>
    </div>
</div>

JS

$(".tooltip-empty-editor").each(function() {
    var index = $(this).closest('.sibling').index(),
        //index = $(this).index(),
        textarea = tinymce.get(index); // get() requires an id or a number

    if( $.trim( textarea.getContent() ) == '' ) {
        // .....
    }
});

如果您的html被父級上的其他元素污染,您將需要使用.filter()來獲取索引。

根據產品文檔,TinyMCE的get() API “按ID返回編輯器實例” id參數應該是一個String 在你的例子中,你似乎試圖傳遞它不是String的“this”對象。

我會給頁面上的每個textarea一個唯一的ID屬性,然后您可以使用該ID引用每個textarea。

正如另一張海報所指出的那樣,您也可以使用整數來訪問頁面上的每個TinyMCE實例(例如tinymce.get(0)tinymce.get(1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM