我試圖讓我的textarea codemirror的值發送到服務器以在編輯後保存它們,但我無法選擇ELEMENT ...我也嘗試通過ID獲取元素...一樣的東西。我得到這個:無法獲取CodeMirror textarea使用js/jquery的多個元素的值
無法讀取空
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="http://esironal.github.io/cmtouch/lib/codemirror.js"></script>
<link rel="stylesheet" href="http://esironal.github.io/cmtouch/lib/codemirror.css">
<script src="http://esironal.github.io/cmtouch/lib/codemirror.js"></script>
<div><textarea class="yyyyyy232" id="scripttextarea_1"></textarea><button type="submit" class='yy4e' id='btn_1'>Save1</button></div>
<div><textarea class="yyyyyy232" id="scripttextarea_2"></textarea><button type="submit" class='yy4e' id='btn_2'>Save2</button></div>
<div><textarea class="yyyyyy232" id="scripttextarea_3"></textarea><button type="submit" class='yy4e' id='btn_3'>Save3</button></div>
<script>
$(document).ready(function() {
myTextarea=document.getElementsByClassName('yyyyyy232');
console.log($(myTextarea).length);
for (i=0;i<myTextarea.length;i++){
cm=CodeMirror.fromTextArea($(myTextarea[i])[0], {
mode: "python",
lineNumbers: true,
lineWrapping: true,
indentUnit: 4,
height: 400
});
};
});
$('.yy4e').on('click',function(e){
e.preventDefault();
ppp=$(this).attr('id').split('_')[1];
xx=document.getElementById("scripttextarea_"+ppp);
cm=CodeMirror.fromTextArea(xx).getValue();
console.log(cm);
$.getJSON('/edit_scripts',{
'command' : cm
},function(data){
console.log('edited');
})
});
</script>
一些幫助,這真是偉大的財產「價值」,因爲這個問題是竊聽我一些好的小時。謝謝
哪條線是由那個錯誤引起的? –
cm = CodeMirror.fromTextArea(xx).getValue();獲取值... – stevenp32
問題是您定義的textarea被CodeMirror代碼覆蓋,實際上您初始化的textarea目前隱藏... – rJ7