2015-03-02 29 views
0

我試圖動態更改textarea的字符串值。在我的代碼setValue函數應該這樣做,但每當我嘗試,文本區域正在改變,但後來不可編輯。 什麼可能導致問題?JQuery如何更改CodeMirror中textarea的字符串值

function createScriptPanel(value, container, mode) { 
     var scriptPanel = $('<div id="editor"><textarea id="editor-code">' + value.script + '</textarea></div>').appendTo(container); 
     CodeMirror.commands.autocomplete = function (cm) { 
      CodeMirror.showHint(cm, CodeMirror.hint.anyword); 
     }; 
     sincapp.codeEditor = CodeMirror.fromTextArea(document.getElementById("editor-code"), {lineNumbers: true, matchBrackets: true, extraKeys: {"Ctrl-Space": "autocomplete"}, mode: mode}); 
     scriptPanel.getValue = function() { 
      return sincapp.codeEditor.getValue(); 
     }; 
     scriptPanel.setValue = function (newName,oldName) { 

      var newValue= sincapp.codeEditor.getValue().replace(newName,oldName); 

      $('#editor:nth-child(1)').html(newValue); 
     }; 

     return scriptPanel; 
    } 

回答

0

因爲你調用HTML而不是VAL 而不是$( '#主編:第n個孩子(1)')HTML(NEWVALUE);如果$('#editor:nth-​​child(1)')。val(newValue);

0

它不是關於textarea,而是關於CodeMirror,我的錯誤。其實這個工作很簡單,對我來說:

sincapp.codeEditor.setValue(newValue) 
0

嘗試這樣的事情,

var newValue = sincapp.codeEditor.getDoc().setValue(newName);