【问题标题】:Get value of textarea created dynamically获取动态创建的textarea的值
【发布时间】:2017-09-14 11:36:05
【问题描述】:

嘿,我有一个 textarea,每次我使用 Google api 的响应按 Enter 时都会自动更新,但是当我尝试从该 textarea 获取值时,它会返回 null 。我知道jsp脚本只运行一次,我不知道每次更改文本区域的文本时如何运行。

<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的脚本就是这个

<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>

【问题讨论】:

    标签: javascript html database jsp


    【解决方案1】:

    您应该使用 .value 更新 textarea 值:

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

    【讨论】:

    • 它会更新值,但是当我尝试获取该值时,它会返回 null,因为 里面的内容只运行一次,这是我重新加载页面的时候
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多