我确定这已被多次询问和回答,但我找不到有效的解决方案。我希望根据第一个菜单中的选择填充第二个菜单:
<form id="form1" name="form1" method="post" action="">
<select name="sub_discipline" id="sub_discipline">
<option>Select Sub-discipline...</option>
<?php
$query = mysql_query("SELECT * from sub_discipline ORDER BY name ASC");
for($i=0;$i<mysql_num_rows($query);$i++) {
$row=mysql_fetch_assoc($query);
?>
<option value="<?php echo $row['sub_discipline_pk']; ?>"><?php echo $row['name']; ?></option>
<?php
}
?>
</select>
<select name="topic_place" id="topic_place">
<option>Select Topic...</option>
</select>
</form>
我有以下js:
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$(':input[name=sub_discipline]').change(function(e) {
$.get(
'get_topics.php',
{'sub_discipline_pk':$(this).val()},
function(data) {
$(':input[name=topic_place]').html( data );
},
'html'
);
}).change();
});
</script>
get_topics.php 有
$sub_discipline_pk = $_GET['sub_discipline_pk'];
$query = "SELECT * FROM topic WHERE sub_discipline_fk = '$sub_discipline_pk'";
$result = mysql_query($query, $connection) or die(mysql_error());
while ($row = mysql_fetch_array($result)) { ?>
<option value="<?php echo $row['topic_pk']; ?>">
<?php echo $row['title']; ?></option>
<?php }
?>
get_topics.php 返回:
<option value="1">
Topic 1</option>
<option value="2">
Topic 2</option>
所以上面应该工作......
谢谢你的帮助!!