更改textarea值不起作用 [英] change textarea value doesn't work
问题描述
所以,我正在尝试做的是在wordpress管理新帖子页面中添加一个Javascript片段,这样当我点击一个按钮时,它会填满帖子正文所在的内容文本区域。添加它不是问题,我已经这样做了,但由于某些原因填写内容字段不起作用。
So, what i'm trying to do is adding a Javascript snippet in wordpress admin new post page, so that when i click a button it fills up the CONTENT textarea where the body of the post goes. Adding it is not a problem, i've already done this, but for some reason filling up the content field doesn't work.
来自wordpress管理页面的textarea是:
The textarea from the wordpress admin page is:
<textarea rows='10' class='theEditor' cols='40' name='content' tabindex='2' id='content'></textarea>
此外,我试图填写POST_TITLE输入,它工作正常。我知道问题可能来自内容字段的wordpress的JS脚本,因为它不是一个简单的textarea,它是一个JS文本编辑器。有什么建议?
和JS片段是:
Also i tried to fill up the POST_TITLE input and it works fine. I know the problem might come from the JS scripts of wordpress for the content field, because it's not a simple textarea, it's a JS text editor. Any suggestions? And the JS snippet is:
<script type="text/javascript">
$("form").submit(function(){
$("input[name$='post_title']").val("a letter");
document.getElementById('content').innerHTML = "testing";
return false;
});
</script>
PS:已包含jquery库
PS: jquery library already included
推荐答案
你不应该混合jQuery和标准的dom调用?试试这个:
You shouldn't mix jQuery and standard dom calls? Try this:
$("#content").text("hi");
从Firebug控制台为我工作正常。
Worked fine for me from the Firebug console.
更新:对不起,伙计们,错过了我们正在讨论的TinyMCE部分。这是用于更新隐藏文本区域和可视化编辑器本身的代码:
Update: Sorry guys, missed the part that it's TinyMCE we're talking about. Here's the code that should be used to update both the hidden textarea AND the visual editor itself:
jQuery("#content").text("Your text goes here");
tinyMCE.activeEditor.execCommand('mceSetContent', false, jQuery("#content").text());
有关详细信息,请查看 TinyMCE Commands API
For more info check out the TinyMCE Commands API
这篇关于更改textarea值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!