我想链接 3 个选择,并使用此代码使其工作。 http://jsfiddle.net/FJFFJ/1/
<div id="filter">
<a id="clone" href="#">+</a> <a id="remove" href="#">-</a>
</div>
<div id="template">
<select class="pais">
<option value="1">Argentina</option>
<option value="2">Chile</option>
</select>
<select class="provincia">
<option value="1" class="1">San Juan</option>
<option value="2" class="1">Mendoza</option>
<option value="3" class="2">La Serena</option>
<option value="4" class="2">Santiago</option>
</select>
<select class="ciudad">
<option value="1" class="1">Rawson</option>
<option value="2" class="2">Godoy Cruz</option>
<option value="3" class="3">Coquimbo</option>
<option value="4" class="4">Chiñihue</option>
</select>
</div>
我现在遇到的问题是,如何从 PHP 中的每个下拉列表中获取选定的值?例如,上面打开页面时会创建 3 个选择框。当我单击“+”按钮并添加更多行选择时,如何从所有动态添加的选择中获取值?
这就是我现在所做的?但仍然无法正常工作。
<?php
if(isset($_POST['submit'])){
foreach($_POST['semester'] as $sem){
echo $sem;
}
}
?>
<!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>Untitled Document</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.js'></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css">
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<script type='text/javascript' src="http://www.appelsiini.net/download/jquery.chained.mini.js"></script>
<style type='text/css'>
#template{
display:none;
}
</style>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
// Form element cloning
var i = 0;
$('#clone').click(function() {
$('#template').clone().appendTo('#filter');
$('#filter #template').attr('id', 'duplicate' + i);
$('#filter div:hidden').show();
chainItWithId(i);
i++;
});
$('#remove').click(function() {
$('#filter > div').last().remove();
});
$('#clone').click();
});
function chainItWithId(id) {
$('#duplicate' + id + ' .department').chained('#duplicate' + id + ' .semester');
$('#duplicate' + id + ' .subject').chained('#duplicate' + id + ' .department');
}
function chainTemp() {
$('#template .department').chained('#template .semester');
$('#template .subject').chained('#template .department');
}
});//]]>
</script>
</head>
<body>
<div id="filter">
<a id="clone" href="#">+</a> <a id="remove" href="#">-</a>
</div>
<form name="request" action="<?php $_SERVER['PHP_SELF']?>" method="post">
<div id="template">
<select class="semester" name="semester[]">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="department" name="department[]">
<option value="EEE" class="1">EEE</option>
<option value="ECE" class="1">ECE</option>
<option value="MECH" class="2">MECH</option>
<option value="CSE" class="2">CSE</option>
</select>
<select class="subject">
<option value="1" class="EEE ECE">S1</option>
<option value="2" class="ECE">S2</option>
<option value="3" class="MECH">S3</option>
<option value="4" class="CSE">S4</option>
<option value="5" class="MECH">S5</option>
<option value="6" class="CSE EEE">S6</option>
</select>
</div>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>