我的代码无效。我试图动态生成元素。怎么做?

时间:2018-03-31 10:08:35

标签: javascript jsp

    <html>
    <head>
    <script type="text/javascript">
    function formQuestion(button_id,question_no)
    {
        document.getElementById(button_id).addEventListener('change',function(){

        var div=document.createElement("div");
        document.body.appendChild(div);

        var textarea=document.createElement("textarea");
        textarea.setAttribute("cols","90px");
        textarea.setAttribute("rows","20px");
        textarea.setAttribute("style","border:solid 4px black; resize:none");
        textarea.setAttribute("id","tid");
        textarea.setAttribute("name","tid");

       //how to append textarea to div ?
        document.body.appendChild(textarea);

        var upload=document.createElement("input");
        upload.setAttribute("type","file");
        upload.setAttribute("onclick","upload(this.id,'tid')");
        document.body.appendChild(upload);

        var buttone=document.createElement("button");
        buttone.setAttribute("type","button");
        document.body.appendChild(buttone);

    }
})
    </script>
    </head>
    <body>
    <br><br><br>
    <% for(int i=0;i<4;i++)
    {%>
    <button type="button" onclick="formQuestions(this.id,'<%= i+1%>')" id=<%= i+1+"button"%>>Question<%= i+1 %></button>
    <br><br><br>    
    <%}
    %>

    </body>
    </html>

我想做什么 - 1.'问题按钮'是根据循环生成的。 2.每个'问题按钮'应创建一个包含文本区域,上传按钮和点击的普通按钮的div。 3.如果我点击其他问题按钮让我们说'问题2'那么问题按钮1的div应该隐藏自己,但内容必须在那里。

请帮忙! 注 - 问题数量=由用户提供。不固定。它各不相同。

1 个答案:

答案 0 :(得分:-1)

如果问题的数量是固定的,那么为什么不直接将它写入html并使用JS代码隐藏/显示何时需要&#39;

相关问题