$('.button-save').click(function(b){
b.preventDefault();
var title = $('#title').val(),
category = $('#category').val(),
file = $('#file').val(),
short_content = tinyMCE.get('#short_content').getContent(),
content = tinyMCE.get('#content').getContent(),
date = $('#date').val(),
language = $('#language').val();
alert(short_content);
$.ajax({
type: 'POST',
url: '/admin/save',
data: {
title:title,
category:category,
file:file,
short_content:short_content,
content:content,
date:date,
language:language
},
success: function(data){
$('.adm-notification').html(data);
}
});
});
如你所见,有两个textareas(short_content,content);我试图通过tinyMCE.get('#content')。getContent()获取它们。但是我收到了错误
未捕获的TypeError:无法调用未定义的方法'getContent'
HTML代码
<form action=\"\" method=\"post\">
<div class=\"element\">
<label for=\"name\">Page title <span class=\"red\">(required)</span></label>
<input id=\"title\" name=\"name\" value=".$edit['title']." class=\"text\" />
</div>
<div class=\"element\">
<label for=\"category\">Category <span class=\"red\">(required)</span></label>
<input id=\"category\" name=\"category\" value=".$edit['category']." class=\"text\" />
</div>
<div class=\"element\">
<label for=\"attach\">Attachments</label>
<input type=\"file\" id=\"file\" name=\"attach\" />
</div>
<div class=\"element\">
<label for=\"short-content\">Short content <span class=\"red\">(required)</span></label>
<textarea name=\"short_content\" id=\"short_content\" class=\"textarea\" rows=\"10\">".$edit['short_content']."</textarea>
</div>
<div class=\"element\">
<label for=\"content\">Long content <span class=\"red\">(required)</span></label>
<textarea name=\"content\" id=\"long_content\" class=\"textarea\" rows=\"10\">".$edit['content']."</textarea>
</div>
<div class=\"element\">
<label for=\"date\">Date <span class=\"red\">(required)</span></label>
<input id=\"date\" name=\"date\" class=\"text\" value=".$edit['date']." />
</div>
<div class=\"element\">
<label for=\"language\">Language <span class=\"red\">(required)</span></label>
<input id=\"language\" name=\"language\" value=".$edit['lang']." class=\"text\" />
</div>
<div class=\"entry\">
<button type=\"submit\" id=\"button-save\" class=\"add button-save\">Save page</button>
</div>
</form>
如何解决?
答案 0 :(得分:0)
您需要提供textarea的id
,而不需要#
。
tinyMCE.get('long_content').getContent()
tinyMCE.get('short_content').getContent()
不过,您需要决定是要提交表单还是使用JavaScript读取值,然后将其发送到服务器。这是一个用JS读出来的解决方案:
<script type="text/javascript">
tinyMCE.init({
mode : "textareas"
});
$(function(){
$('#button-save').click(function(){
console.log("ok");
var short_content = tinyMCE.get('short_content').getContent();
var content = tinyMCE.get('long_content').getContent();
alert(short_content);
alert(content);
});
});
</script>
<textarea name="short_content" id="short_content" >'short_content'</textarea>
<textarea name="long_content" id="long_content" >'content'</textarea>
<button id="button-save" class="add button-save">Save page</button>