java - 选择选项后无法创建输入文本框

标签 java javascript html jsp

我是网络开发新手,我尝试从互联网上寻找任何资源,但我仍然无法弄清楚我的问题......

=> 读取文本文件并生成选项... 我的 test.jsp 页面

<tr>
<td>Select test : </td>
<td><select id="testname" name="testname" onchange="makeBox()">
while ((src = test.readLine()) != null){
    param = "";
    temp =src.split(":");
    tempsize =temp.length;
    if ((tempsize >2)){
        int i;
        for (i=2; tempsize>i ; i++){
            if((temp[i].equals("null"))){
                 param = "";
            }
            else if ((i ==2) && (temp[i] != "null")){
                 param = temp[i];
            }
            else if ((i > 2)){
                 param = param + "," + temp[i];
            }
         }
     }
      %>
     <option value="<%=param%>" name="<%=temp[0]%>"><%=temp[0]%></option>
     <%
}
test.close();
    %>
</select></td>
</tr>

当用户选择其中一个选项时,javascript将在输入文本框中生成相应的参数...(参数数量因测试而异)

例如。让我们说

<option value="size,height,weight" name=apple>apple</option>

已选择。然后我希望我的页面显示 3 个文本框,例如......

<td> Enter size: </td>
<td><input type="text" id="size" name="size"></td>
<td> Enter height: </td>
<td><input type="text" id="height" name="height"></td>
<td> Enter wieght: </td>
<td><input type="text" id="weight" name="weight"></td>

我的尝试:

<script type="text/javascript">
    function makeBox() {
        var selected = document.getElementById('batchname');
        for (var i=1, n=selected.options.length;i<n;i++){
            if (selected.options[i].value){
                var a = "<input type = 'text' name = '" + selected.options[i].value + "' id = '" + selected.options[i].value + "'>";
                document.getElementById("inputBox").innerHTML = a;
            }
        }
    };
    </script>

我正在读取的文本文件:

Apple:A.B.C.D:size:colour
Banana:G.C.D.E:length
Pear:L.D.E.F:shape:weight:color
Orange:E.C.A.W:density:length:color:weight
Melon:E.P.D.A                    // no param

^ 没有做任何事情... 从选择框中发送多个值来创建多个输入框是根本错误的吗? 如果有更好的方法,请告诉我...:) 感谢您的帮助!

最佳答案

您必须向选择字段添加事件监听器,并在值更改时创建一个元素。这是一些可能对您有帮助的小代码。我只是写了一个粗略的代码来给你一个想法。您必须解决它才能使其适用于您的特定情况。

一旦加载正文,您就必须设置此事件监听器。所以在你的 HTML 上

<body onload="init()" id="stage" class="theme">

以及你的 JavaScript

function init() {
   document.getElementById("selectListID").addEventListener("change", function(){
      var value = document.getElementById("selectListID").value; // this gives you the selected value.
      // Your code to add the element that you need.
   };)
}

希望这有帮助。 希望这会有所帮助。

关于java - 选择选项后无法创建输入文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13571594/

相关文章:

java - java JDBC +mysql数据库更新查询

javascript - Express 4 奇怪的动态中间件行为

html - Internet Explorer 中的 Segoe UI 字体

javascript - 如何从 iframe 中取出菜单并显示在两个 iframe 上?

php - 在设定的时间段内显示页面元素

java.lang.IllegalStateException : failed to create a child event loop

java - 在代码中删除XML文件中的内容

java - 从 Php 执行 Java 应用程序

javascript - execCommand中“粘贴为纯文本”的JavaScript技巧

javascript - 需要从javascript中的2个数组中获取