javascript 使用jquery获取受tinymce编辑器控制的textarea的值而不发送表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6830541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:00:41  来源:igfitidea点击:

Getting value of textarea which is under control of tinymce editor using jquery without sending form

javascriptjquerytinymce

提问by David

I have some textarea

我有一些文本区域

<textarea name='text' id='text' ></textarea>

which is under control of tinymce script.

这是在 tinymce 脚本的控制之下。

And I have simple javascript

我有简单的 javascript

alert ($('#text').val());

which gives me nothing. Problem is that tinymce is converting textarea into something , so actually there is no textarea with id 'text' anymore. Because of that javascript gives empty alerts when I press submit button even if there is some text typed in textarea.

这给了我什么。问题是 tinymce 正在将 textarea 转换为 something ,因此实际上不再有 ID 为 'text' 的 textarea 了。因为即使在 textarea 中输入了一些文本,当我按下提交按钮时,javascript 也会给出空警报。

So question is how can javascript get the value of such textarea on the fly whenever I need it ?

所以问题是 javascript 如何在我需要时即时获取此类 textarea 的值?

回答by Felix Kling

It should be possible using the get[docs]and getContent[docs]methods:

应该可以使用get[docs]getContent[docs]方法:

var value = tinymce.get('text').getContent();

回答by Claudio

There is a method of the TinyMCE instance that can "sync" the TinyMCE content (an IFRAME) with your textarea. I think the method is triggerSave(). Look also here: Need to autosave TinyMCE

TinyMCE 实例有一种方法可以将 TinyMCE 内容(一个 IFRAME)与您的文本区域“同步”。我认为该方法是 triggerSave()。也看这里:需要自动保存 TinyMCE