0

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

=> 读取文本文件并生成选项...我的 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

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

4

1 回答 1

1

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

您必须在加载正文后立即设置此事件侦听器。所以在你的 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.
   };)
}

希望这可以帮助。希望这可以帮助。

于 2012-11-26T19:30:51.437 回答