如何根据必须生成的次数生成多组下拉列表和文本类型输入字段(其中一组包含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 =文本输入字段和一个REMOVE按钮,以防某些生成超过要求。
这正是我需要的,但不同之处在于我需要它是一个自动化过程。
我需要的是(根据我的代码):
如果从第一个下拉列表“计算机设备数量”中选择05,它应该生成05组保管箱并输入=文本输入并删除按钮。
它应该生成05集,其中01 set = 03 dropdowns + 01 type=text
输入字段和删除按钮(如果生成超过requiremt)。
答案 0 :(得分:1)
为您的代码添加一个循环。
喜欢这个
<强> 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>
答案 1 :(得分:0)
只是提示:您可以替换:
> <select name="optone" ... onchange="setOptions(document.add_new_customer_form.optone.options[document.add_new_customer_form.optone.selectedIndex].value,document.add_new_customer_form.opttwo);">
使用:
<select name="optone" ... onchange="setOptions(this.value, this.form.opttwo)">
与其他听众类似。
我还建议你摆脱XHTML过渡DOCTYPE,显然你没有过渡到XHTML(自XHTML 1.0以来已经过去了12年,很少有网站将文档作为XML提供,无论DOCTYPE如何) ,只需使用DOCTYPE html
。
答案 2 :(得分:0)
我不确定我是否理解您的所有代码,但让我看看我是否理解您的问题:
您有一个表单,您希望用户从下拉框中选择多个设备。用户选择一个数字后,您需要填充相应数量的表单元素。
为什么不将你的一些代码合并到this example。