如何使用下拉菜单打开新的动态表单[选择标签]

时间:2013-03-20 08:36:34

标签: javascript jquery html css forms

我会尝试尽可能具体。我有一个下拉菜单[select tag],它允许用户选择5个选项;即从1到5的数字。现在,根据所选的选项,我想显示一个与所选选项具有相同数量的输入标签的新表格

因此,如果用户从下拉菜单中选择3,则底部将显示一个子表,显示三个输入标签。代码是:

<select id="formName9" name="blockUnits">
 <option selected="selected" value="1">1</option>
 <option value="2">2</option>
 <option value="3">3</option>
 <option value="4">4</option>
 <option value="5">5</option>
</select>

6 个答案:

答案 0 :(得分:7)

您将使用on change事件监听器:

$('select#formName9').on('change', function() {
    /* Remove the existing input container, if it exists. */
    $('form#container').remove();

    /* Get the selected value and create the new container. */
    var val = $(this).val()
        $container = $('<form id="container"></form>');

    /* Loop through creating input elements based on value. */
    for(i=0;i<val;i++)
    {
        /* Create the new input element. */
        var $input = $('<input type="text"/>');

        /* Append this input element to the container. */
        $input.appendTo($container);
    }

    /* Add container to the page. */
    $container.insertAfter($(this));
})

JSFiddle example

然后,您可以对此进行扩展,以根据最初的selected选项添加默认输入:

Extended JSFiddle

答案 1 :(得分:7)

我支持James Donnelly的回答。如果您希望它是纯Java脚本,您可以使用此替代方法。

<强> HTML

 <select id="formName9" name="blockUnits" onchange="addInput()">
     <option selected="selected" value="1" >1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option>
    </select>
    <form>
    <div id="newAdd"> </div>.
    </form>

<强> JAVASCRIPT

function addElement() {
    var element = document.createElement("input"); //creating input
    element.setAttribute("value", "");//setting its value
    element.setAttribute("name", "newInput");//naming the input
    var foo = document.getElementById("newAdd");
    foo.appendChild(element);//appendign the value into the parant div
}
function addInput(){
    document.getElementById("newAdd").innerHTML="";//clearing the div
     var noInp=parseInt(document.getElementById("formName9").value);
        while(noInp>0)
        {
        addElement();
            noInp--;
        }           
    }

这是JSFIDDLE

答案 2 :(得分:0)

$('select#formName9').on('change', function() {
var ddVal=$(this).val();
for(i=0;i<ddVal.length;i++)
{
$("#FORMPOSITION").append("<input type='text' name='ddG"+i+"' id='ddVL"+i+"'>");
}
});

在执行前考虑引号。

答案 3 :(得分:0)

我尝试过并且工作过。试试吧:

        $(document).ready(function() {
            $('#formName9').change(function() {
                var number = parseInt($('#formName9').val());
                if(number == 3) {
                    alert('3 HERE');
                }
            });
        });

答案 4 :(得分:0)

这是使用jQuery和CSS的一种动态示例。我提供了working jsFiddle.

这是基本的想法,你可以解决这个问题,并在需要时添加更多输入。

HTML:

<select id="formName9" name="blockUnits">
    <option selected="selected" value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>
<div id="form1" class="hiddenForm form">Form 1</div>
<div id="form2" class="hiddenForm form">Form 2</div>
<div id="form3" class="hiddenForm form">Form 3</div>
<div id="form4" class="hiddenForm form">Form 4</div>
<div id="form5" class="hiddenForm form">Form 5</div>

jQuery的:

$("#form1").show()
$("#formName9").change(function () {
    var form = "#form" + $(this).val();
    $(form).siblings(".hiddenForm").hide()
    $(form).show();
});

CSS:

.form {
    padding: 10px;
    border: 1px dotted #000;
    margin: 5px;
}
.hiddenForm {
    display: none;
}

答案 5 :(得分:-1)

您可以使用Ajax调用,并将响应添加到DOM,或使用JS直接添加DOM。

对于直接DOM,有些想法(使用jQuery):

$('#formName9').on('change', function() {
   var selected = $(this).val();
   $('#aDomElement').empty();
   for (var i=1; i<=selected; i++) {
       $('#aDomElement').append(
           $('<div id="'+i+'">').html(
                   $('<input>').attr('name', 'input'+i).attr('type', 'text')
           )
        );
   }
});

其中aDomElement是现有HTML元素的ID。