2017-09-14 25 views
0

ちょっと私はGoogleのAPIからの応答でenterを押すたびに自動的に更新されるテキストエリアを持っていますが、そのテキストエリアから値を取得しようとするとnullが返されます。私はJSPスクリプトが1回だけ実行されることを知っています。テキストエリアのテキストが変更されるたびに実行する方法がわかりません。動的に作成されたtextareaの値を取得

<div id="main-wrapper"> 
    <div class="container"> 
     <div class="row"> 
      <div class="col"> 
       <div class="input-field" id="input"> 
        <input placeholder="Hey, ask me something..." id="q" 
         type="text" type="email" class="form-control"     
         onKeyDown="if(event.keyCode==13) actionApi();"> 
       </div> 
       <div id="result"></div> 
      </div> 

      <div class="col s5"> 
       <h5>Response payload:</h5> 

       <h1 id="test"></h1> 
       <textarea id="jsonResponse" name="parametru"> 
       <% 
         Connection conn = null; 
         try { 
          Class.forName("com.mysql.jdbc.Driver"); 
          conn = DriverManager.getConnection("jdbc:mysql://localhost/api?user=root&password=1234"); 
         } catch (SQLException e) { 
          e.printStackTrace(); 
         } catch (ClassNotFoundException e) { 
          e.printStackTrace(); 
         } 
         String res = request.getParameter("parametru"); 
         out.println(res); 

         try (PreparedStatement insert = (PreparedStatement) conn 
           .prepareStatement("insert into test(Id, nume) value(default, ?)")) { 
          if (res != null) 
           insert.setString(1, res); 
          else 
           insert.setString(1, "aaa"); 
          insert.execute(); 
         } catch (SQLException e) { 
          e.printStackTrace(); 
         } 
        %> 
        </textarea> 


       </div> 



      </div> 
     </div> 
    </div> 

およびTextAreaを更新するスクリプトがあなたが.valueを使用してテキストエリアの値を更新する必要があり、この

<script> 
    function actionApi() { 
     setTimeout(
       function() { 
        var node = document.getElementsByClassName("raspuns")[document 
          .getElementsByClassName("raspuns").length - 1]; 
        textContent = node.textContent; 
        document.getElementById("jsonResponse").innerHTML = textContent; 
        console.log(textContent); 
       }, 1500); 
    } 
</script> 

答えて

0

です:

document.getElementById("jsonResponse").value = textContent; 
+0

これは、値を更新しますが、私はその値を取るしようとすると、 <% AND %>は一度だけ実行され、これはページがリロードされたときのものなので、ヌルを返します。 –

関連する問題