使用javascript </select>动态添加<select>框

时间:2011-03-17 15:27:41

标签: javascript xhtml

我有一个用户选择文件的表单,然后应该能够选择他们上传的报告有多少参数

我希望有一个数字为0 -5的下拉菜单,当用户选择一个数字时,会显示许多带参数类型的框,例如带有“date”,“name”,“part#”的下拉菜单等等...在选择菜单中,为用户选择的每个号码做出

我查看了S.O.的其他解决方案。他们有意义,但我似乎无法让他们工作,

这是我到目前为止只是为了测试是否会出现一个框:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:c="http://java.sun.com/jsp/jstl/core">


<h:head>

</h:head>

<body>



    <ui:composition template="./templates/template.xhtml">

        <ui:define name="content">
            <c:choose>
                <c:when test="#{controls.designAuth}">
                Welcome Report Designer!<br /><br />

                <div id="fileUpload">



                    <form name="myForm" action="../UploadServlet" enctype="multipart/form-data" onsubmit="return validate_form(this);" method="POST">

                        <b>Make sure your filename is meaningful (eg. WasteByMachineReport.jrxml)</b><br /><br />

                        Please specify a file:<input type="file" name="file" size="40"/><br />
                        Number of parameters: <select name='numSelect' onchange="draw_param_dropdowns();">
                                                    <option>0</option>
                                                    <option>1</option>
                                                    <option>2</option>
                                                    <option>3</option>
                                                    <option>4</option>
                                              </select><br />

                                              <select class="parametersHide"  id="select1"><option></option></select>
        <script type="text/javascript" language="Javascript">


                                                    function draw_param_dropdowns(){

                                                      var sel = document.getElementById("select1");
                                                      sel.style = "visibility:visible";
                                                    }

           </script>

                        <input type="submit" value="Upload"/>

                    </form>

                </div>
                </c:when>
                <c:otherwise>
                    Not Authorized 
                </c:otherwise>
            </c:choose>

        </ui:define>

    </ui:composition>

</body>
</html>

“查看来源”结果:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="./../resources/css/cssLayout.css" rel="stylesheet" type="text/css" />
    <link href="./../resources/dtree/dtree.css" rel="stylesheet" type="text/css" />


    <script type="text/javascript" src="./../resources/dtree/dtree.js"></script>
    <title>Reports</title></head><body>


    <div id="top" class="top">

            <h1>Wingfoot Reports v0.3
                <div style="float:right;margin-right: 20px;">
<form id="j_idt8" name="j_idt8" method="post" action="/WebApplication8/faces/designer.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt8" value="j_idt8" />
<span style=" font-size: 20px; font-weight: normal;">SCOTT </span><input type="submit" name="j_idt8:j_idt10" value="Logout" style=" font-size: 20px;" /><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="-4683216470766096399:-9055922898460672452" autocomplete="off" />
</form>
                </div>
            </h1>
    </div>
    <div>
        <div id="left">

<form id="j_idt14" name="j_idt14" method="post" action="/WebApplication8/faces/designer.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt14" value="j_idt14" />

                        <center>
<script type="text/javascript" src="/WebApplication8/faces/javax.faces.resource/jsf.js?ln=javax.faces&amp;stage=Development"></script>
<a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt14'),{'j_idt14:j_idt16':'j_idt14:j_idt16'},'');return false">Design</a> | <a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt14'),{'j_idt14:j_idt18':'j_idt14:j_idt18'},'');return false">Admin</a> | <a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt14'),{'j_idt14:j_idt20':'j_idt14:j_idt20'},'');return false">Queries</a><br />
                        <hr />

                        </center><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="-4683216470766096399:-9055922898460672452" autocomplete="off" />
</form>
        </div>
        <div id="content" class="left_content">
                Welcome Report Designer!<br /><br />

                <div id="fileUpload">



                    <form name="myForm" action="../UploadServlet" enctype="multipart/form-data" onsubmit="return validate_form(this);" method="POST">

                        <b>Make sure your filename is meaningful (eg. WasteByMachineReport.jrxml)</b><br /><br />

                        Please specify a file:<input type="file" name="file" size="40" /><br />
                        Number of parameters: <select name="numSelect" onchange="draw_param_dropdowns();">
                                                    <option>0</option>
                                                    <option>1</option>
                                                    <option>2</option>

                                                    <option>3</option>
                                                    <option>4</option>
                                              </select><br />

                                              <select class="parametersHide" id="select1"><option></option></select>
<script type="text/javascript" language="Javascript">


                                                        function draw_param_dropdowns(){

                                                      var sel = document.getElementById("select1");
                                                      sel.style = "visibility:visible";
                                                    }

    </script>

                        <input type="submit" value="Upload" />

                    </form>

                </div>
        </div>
    </div>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

您可以尝试在某种循环中对样式标记进行修改,因为看起来您希望用户输入内容,我会使用输入标记。
例如,你有:

<select name="numSelect" id = "numSelect" onchange="draw_param_dropdowns();">
                                                <option>0</option>
                                                <option>1</option>
                                                <option>2</option>
                                                <option>3</option>
                                                <option>4</option>
                                          </select>
<!-- Create all the boxes for input and hide them initially -->
<select name="select1" id="select1" style="display:none;"><option></option></select>
<select name="select2" id="select2" style="display:none;"><option></option></select>
...

并为您的javascript:

<script type="text/javascript" >
//Loop through the drop down boxes and hide them all.
//This will ensure that you don't have 'leftover' boxes after selecting
function draw_param_dropdowns() {
        for (var x = 1; x <= 4; x++) {
            document.getElementById('select' + x).style.display='none';
        }

        for (var y = 1; y <= document.getElementById("numSelect").selectedIndex; y++) {
            document.getElementById('select' + y).style.display = 'block';
        }
    }
</script>

它可能比SO的其他一些程序员可以做的要长一些,但希望这会带来更多的理解。