简体   繁体   English

更改textarea值不起作用

[英]change textarea value doesn't work

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. 因此,我想做的是在wordpress admin的新帖子页面中添加一个Javascript代码段,以便当我单击一个按钮时,它会填充帖子正文所在的CONTENT textarea。 Adding it is not a problem, i've already done this, but for some reason filling up the content field doesn't work. 添加它不是问题,我已经做到了,但是由于某种原因,填充内容字段无效。

The textarea from the wordpress admin page is: 来自WordPress管理页面的textarea是:

<textarea rows='10' class='theEditor' cols='40' name='content' tabindex='2' id='content'></textarea>

Also i tried to fill up the POST_TITLE input and it works fine. 我也试图填补POST_TITLE输入,它工作正常。 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. 我知道问题可能来自wordpress的content字段的JS脚本,因为它不是简单的textarea,而是JS文本编辑器。 Any suggestions? 有什么建议么? And the JS snippet is: JS片段是:

<script type="text/javascript">

    $("form").submit(function(){
        $("input[name$='post_title']").val("a letter");
        document.getElementById('content').innerHTML = "testing";
        return false;
    });

</script>

PS: jquery library already included PS:jquery库已包含

Use .val() to set (or get) the value of an input type element, including <textarea> and <select> , like this: 使用.val()设置(或获取)输入类型元素的值,包括<textarea><select> ,如下所示:

$("form").submit(function(){
    $("input[name$='post_title']").val("a letter");
    $('#content').val("testing");
    return false;
});

You shouldn't mix jQuery and standard dom calls? 您不应该混合使用jQuery和标准dom调用吗? Try this: 尝试这个:

$("#content").text("hi");

Worked fine for me from the Firebug console. 从Firebug控制台对我来说效果很好。

Update : Sorry guys, missed the part that it's TinyMCE we're talking about. 更新 :对不起,我们错过了我们正在谈论的TinyMCE。 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());

For more info check out the TinyMCE Commands API 有关更多信息,请查看TinyMCE Commands API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM