javascript - 如何根据下拉列表中选择的数字多次执行相同的操作?

标签 javascript jquery dynamic

如何生成多组下拉菜单和文本类型输入字段(其中一组包含 3 个下拉菜单和 1 个输入类型 = 文本),根据必须生成的次数是从另一个下拉菜单中选择一个数字.

我的代码是这样的:

<!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" />
<title>Add New Detail</title>
<link href='http://fonts.googleapis.com/css?family=Orienta' rel='stylesheet' type='text/css'>

<style type="text/css">
#main {
    background-color:#0CF;
    width:600px;
    padding:25px 0;
}

#drop_box_set {
    background:#666;
    width:90%;
    color:#FFF;
    padding:25px 0;
}

</style>
<!------------------------------------------------------------------------------------------------>
<!--------------------------- CHANGE OPTIONS ON CHILD DROPDOWN SCRIPT ---------------------------->
<!------------------------------------------------------------------------------------------------>

<script type="text/javascript">
function setOptions(chosen, selbox) {

selbox.options.length = 0;
if (chosen == " ") {
  selbox.options[selbox.options.length] = new Option('Please select one of the options above first',' ');
setTimeout(setOptions(' ',document.add_new_customer_form.optthree),5);
}
if (chosen == "1") {
  selbox.options[selbox.options.length] = new Option('first choice - option one','11');
  selbox.options[selbox.options.length] = new Option('first choice - option two','12');
setTimeout(setOptions('11',document.add_new_customer_form.optthree),5);
}
if (chosen == "2") {
  selbox.options[selbox.options.length] = new Option('second choice - option one','21');
  selbox.options[selbox.options.length] = new Option('second choice - option two','22');
setTimeout(setOptions('21',document.add_new_customer_form.optthree),5);
}
if (chosen == "3") {
  selbox.options[selbox.options.length] = new Option('third choice - option one','31');
  selbox.options[selbox.options.length] = new Option('third choice - option two','32');
setTimeout(setOptions('31',document.add_new_customer_form.optthree),5);
}
if (chosen == "11") {
  selbox.options[selbox.options.length] = new Option('first choice - option one - sub one','111');
  selbox.options[selbox.options.length] = new Option('first choice - option one - sub two','112');
}
if (chosen == "12") {
  selbox.options[selbox.options.length] = new Option('first choice - option two - sub one','121');
  selbox.options[selbox.options.length] = new Option('first choice - option two - sub two','122');
}
if (chosen == "21") {
  selbox.options[selbox.options.length] = new Option('second choice - option one - sub one','211');
  selbox.options[selbox.options.length] = new Option('second choice - option one - sub two','212');
}
if (chosen == "22") {
  selbox.options[selbox.options.length] = new Option('second choice - option two - sub one','221');
  selbox.options[selbox.options.length] = new Option('second choice - option two - sub two','222');
}
if (chosen == "31") {
  selbox.options[selbox.options.length] = new Option('third choice - option one - sub one','311');
  selbox.options[selbox.options.length] = new Option('third choice - option one - sub two','312');
}
if (chosen == "32") {
  selbox.options[selbox.options.length] = new Option('third choice - option two - sub one','321');
  selbox.options[selbox.options.length] = new Option('third choice - option two - sub two','322');
}
}
</script>

</head>

<body>

<div align="center"><!--DIV ALIGNMENT CENTER START-->

<div id="main"><!--DIV MAIN START-->

<form name="add_new_customer_form" method="post" action="">

<label class="description">Number Of Computer Devices : </label>

            <select name="element_19" class="short_select"> 
                <option value="" selected="selected"></option>
                        <option value="01" >01</option>
                        <option value="02" >02</option>
                        <option value="03" >03</option>
                        <option value="04" >04</option>
                        <option value="05" >05</option>
                        <option value="06" >06</option>
                        <option value="07" >07</option>
                        <option value="08" >08</option>
                        <option value="09" >09</option>
                        <option value="10" >10</option>
                        <option value="11" >11</option>
                        <option value="12" >12</option>
                        <option value="13" >13</option>
                        <option value="14" >14</option>
                        <option value="15" >15</option>

        </select>
<div id="drop_box_individual"><!--DIV DROP_BOX_INDIVIDUAL START-->
<label class="description">Device Type : </label>

            <select name="optone" class="element select medium" style="width:200px;" onchange="setOptions(document.add_new_customer_form.optone.options[document.add_new_customer_form.optone.selectedIndex].value,document.add_new_customer_form.opttwo);">
                <option value=" " selected="selected"> </option>
                        <option value="1">First Choice</option>
                        <option value="2">Second Choice</option>
                        <option value="3">Third Choice</option>
            </select>

</div><!--DIV DROP_BOX_INDIVIDUAL ENDS-->

<div id="drop_box_individual"><!--DIV DROP_BOX_INDIVIDUAL START-->
<label class="description">Device Brand : </label>

            <select name="opttwo" class="element select medium" style="width:200px;" onchange="setOptions(document.add_new_customer_form.opttwo.options[document.add_new_customer_form.opttwo.selectedIndex].value,document.add_new_customer_form.optthree);">
                <option value=" " selected="selected"></option>
            </select>
</div><!--DIV DROP_BOX_INDIVIDUAL ENDS-->

<div id="drop_box_individual"><!--DIV DROP_BOX_INDIVIDUAL START-->
<label class="description">Operating System : </label>

            <select name="optthree" class="element select medium" style="width:200px;">
                <option value=" " selected="selected"></option>
            </select>

</div><!--DIV DROP_BOX_INDIVIDUAL ENDS-->

<div id="drop_box_individual"><!--DIV DROP_BOX_INDIVIDUAL START-->
<label class="description">Serial Number : </label>

            <input name="element_10" class="element text medium" type="text" maxlength="255" value=""/>
</div><!--DIV DROP_BOX_INDIVIDUAL ENDS-->
</form>
</div><!--DIV MAIN ENDS-->
</div><!--DIV ALIGNMENT CENTER ENDS-->

</body>
</html>

另外,这里有一个链接 jsFiddle我在寻找解决方案时遇到了。

在上面的 jsFiddle 中,如果单击“添加”按钮,它会生成一组 02 下拉菜单 + 01 type=text 输入字段和一个 REMOVE 按钮,以防某些人生成的内容超出要求。

这正是我所需要的,但不同之处在于我需要它是一个自动化过程。

我需要的是(根据我的代码):

如果从说“计算机设备数量”的第一个下拉列表中选择 05,它应该生成 05 组下拉框和 type=text 输入并删除按钮。

它应该生成 05 个集合,其中 01 set = 03 dropdowns + 01 type=text 输入字段和一个删除按钮(如果生成的数量超过要求)。

最佳答案

在您的代码中添加一个循环。

像这样

JS

$(function() {
    $("input[type=button][value=Add]").click(function(e) {
        for (i = 0; i < document.getElementById('sel').value; i++) {
            e.preventDefault();
            var newDiv = $("<div>").appendTo("#div");
            $("<input>").attr("name", "a").appendTo(newDiv);
            $("<input>").attr("name", "b").appendTo(newDiv);
            $("<select>").attr("name", "c").appendTo(newDiv).append(
            $("<option>").val("0").text("Option 1"), $("<option>").val("1").text("Option 2"));
            $("<select>").attr("name", "d").appendTo(newDiv).append(
            $("<option>").val("0").text("Option 1"), $("<option>").val("1").text("Option 2"));
            $("<button>").text("Remove").appendTo(newDiv).click(function(e) {
                e.preventDefault();
                newDiv.remove();
            })
        }
    })
})

HTML

<form>
    <div id="div"></div>
    <select id="sel">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    <input type="button" value="Add" />
</form>

在这里查看它的工作情况:http://jsfiddle.net/RASG/bYBPD/

关于javascript - 如何根据下拉列表中选择的数字多次执行相同的操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12683260/

相关文章:

javascript - 如何在从函数(实际上是过滤器)返回的元素上使用 ngRepeat - AngularJS?

javascript - 'Wack-A-Mole' 风格游戏的多个 jQuery 问题

javascript - 按 .elements id 对 .container .elements 进行排序

c# - 我应该使用哪种设计模式来动态地为对象提供不同的效果?

dynamic - 动态加载 div 时使 iscroll 起作用

javascript - 匹配单词和句号,但不匹配结尾的句号

javascript - 页面加载后创建的 DOM 元素不触发 Jquery 事件

c++ - Int& 到 const int -static 与 dynamic-

javascript - 上传前的 CSS 方形缩略图预览

javascript - 对象内部的函数无法在 JavaScript 中访问该对象