2010-01-10 16 views
9

那麼,我首先編寫了一個Javascrip函數,根據您在下拉框中所做的選擇,改變textarea中的文本,這非常簡單。一旦CKeditor實例被調用,Javascript不能更改textarea中的文本

HTML

<form name="formconteudo"> 
<select name="selectpage" onChange="change();"> 
<option value="1">something</option> 
<option value="2">another thing</option> 
<option value="3">going crazy</option> 
</select> 
</form> 

JS

var Code = new Array("", "Selected 1", "Selected 2", "Selected 3"); 
function change() 
{ 
var ID = formconteudo.selectpage.options[formconteudo.selectpage.selectedIndex].value; 
document.formconteudo.ckeditor.value = Code[ID]; 
} 

這個工作不錯,並改變在textarea的文字。但後來我在該textarea上調用了CKeditor實例,以便我可以在該textarea上使用CKEditor。編輯器加載得很好,效果很好。但現在的JavaScript不工作。

有關這個問題的任何提示?

感謝

回答

26

你將要使用的方法setData在編輯器。

這裏is the example from their docs

CKEDITOR.instances.editor1.setData('<p>This is the editor data.</p>'); 

這意味着你的代碼會是這個樣子:

var Code = new Array("", "Selected 1", "Selected 2", "Selected 3"); 
function change() 
{ 
var ID = formconteudo.selectpage.options[formconteudo.selectpage.selectedIndex].value; 
CKEDITOR.instances.editor1.setData('<p>' + Code[ID] + '</p>'); 
} 

注意instances.editor1可能不是指你的盒子,所以一定要使用正確的名稱

+0

Bingo,就是這樣。謝謝。 – 2010-01-10 22:29:53

+0

謝謝。你也一樣。 – 2010-01-10 22:34:26

2

我已經在這個問題上花了幾天時間,每個人都不斷給我提供奇怪的解決方案檢查了他們的API,甚至舉了一個例子。

http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#setData

CKEDITOR.instances.YOUREDITORID.updateElement(); 
    alert(document.getElementById('YOUREDITORID').value); // The current editor data. 

'YOUREDITORID'是textarea的用於CKEDITOR的ID來使用。

希望這會有所幫助!

相關問題