【问题标题】:unable to create input text box when option is selected选择选项时无法创建输入文本框
【发布时间】:2012-11-26 19:20:15
【问题描述】:

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

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

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

【问题讨论】:

    标签: java javascript html jsp


    【解决方案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.
       };)
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-17
      • 2013-06-29
      • 1970-01-01
      • 2021-09-02
      • 2011-09-18
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      相关资源
      最近更新 更多