2016-11-21 52 views
0

我試圖讓我的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>   

一些幫助,這真是偉大的財產「價值」,因爲這個問題是竊聽我一些好的小時。謝謝

+0

哪條線是由那個錯誤引起的? –

+0

cm = CodeMirror.fromTextArea(xx).getValue();獲取值... – stevenp32

+0

問題是您定義的textarea被CodeMirror代碼覆蓋,實際上您初始化的textarea目前隱藏... – rJ7

回答

1

$(document).ready(function() { 
 
      myTextarea=document.getElementsByClassName("yyyyyy232"); 
 
      console.log($(myTextarea).length); 
 
      var cm = new Array(); 
 
       for (i=0;i<myTextarea.length;i++){ 
 

 
        cm[i]=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]; 
 
          numcode= parseInt(ppp)-1; 
 
          xx=$("#scripttextarea_"+ppp).attr("id"); 
 
          console.log(xx); 
 

 

 
           var Code=cm[numcode].getValue(); 
 
           console.log(Code); 
 

 
          $.getJSON("/test",{ 
 
           "command" : cm[numcode] 
 
          },function(data){ 
 
           console.log("edited"); 
 
          }) 
 
         }); 
 

 

 
    });
<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> 
 

這是 你有幾個問題,一個是提及上述有關BTN名稱上點擊 秒掉的文件代碼的工作示例準備 第三你識別作爲所有代碼鏡子值cm,它應該是數組cm [] 您需要通過cm []調用文本區域值數組編號

+0

完美的答案。這個人你救了我! – stevenp32

+0

歡迎任何時間:) –

0

入住這裏:

ppp=$(this).attr('id').split('_')[1]; 

而且在這裏看到:

<div> 
    <textarea class="yyyyyy232" id="scripttextarea_1"></textarea> 
    <button type="submit" class='yy4e' id='btn1'>Save1</button> 
</div> 

BTN1不能被拆分 '_',這將是罰款,你替換BTN1bth_1

提醒:您可以嘗試首先自己逐步打印出數值。

+0

編輯我的問題......它應該是與btn_1,它不能這樣工作...我找到正確的ID ...並使用cm = CodeMirror.fromTextArea(的document.getElementById(XX))的getValue(); - 同樣的東西 – stevenp32

+0

你現在的問題是什麼? – Anson

+0

如何獲取codemirror textarea的值? – stevenp32