[英]Display second dropdown list if a value is selected in the first dropdown (using variables)?
Is there any way I can select a certain value in a dropdown list and according to that selection display a second dropdown list? 有什么方法可以在下拉列表中选择某个值,然后根据该选择显示第二个下拉列表? I know how to do it with just normal words, but what if I use variable names? 我知道只用普通的单词怎么做,但是如果我使用变量名怎么办?
University Name: <select name="university" id="university">
<?php
while($row = mysql_fetch_array($uniName)) {
echo "<option>" . $row['uni_name'] . "</option>";
}
?>
</select>
<br/>
Course Name: <select name='course' id='course'>
<?php
while($row = mysql_fetch_array($courseNames)) {
echo "<option>" . $row['course_name'] . "</option>";
}
?>
</select>
So as you can see, in the first dropdown list I've added all the retrieved university names from my database. 如您所见,在第一个下拉列表中,我从数据库中添加了所有检索到的大学名称。 I did the same for the courses as well. 我的课程也一样。
Now how can I modify the code below: 现在如何修改下面的代码:
$(document).ready(function() {
$('#course').hide();
$('#university').change(function () {
if ($('#university option:selected').text() == "Harvard University"){
$('#course').fadeIn('slow');
}
else {
$('#course').fadeOut('slow');
}
});
});
Instead of "Harvard University", I want to put a variable there according to what the user has selected. 我想根据用户选择的内容在此处放置一个变量,而不是“哈佛大学”。 Something like $university_name and then I'll create a new query and display only the courses belong to a certain $university_name. 像$ university_name这样的东西,然后我将创建一个新查询并仅显示属于某个$ university_name的课程。
As already stated in my comments, I recommend using AJAX to fill the course
box. 如我的评论中所述,我建议使用AJAX填充course
框。
$.ajax("getCourses.php", {
data: { UID: uid },
type: 'POST',
done: function(result) {
$("#course").html(result);
});
This will fill the select tags with whatever getCourses
has for output. 这将用getCourses
可以输出的内容填充选择标签。
UID would of course be the ID of the university in question. UID当然是有关大学的ID。
This page has all the info you need. 此页面包含您需要的所有信息。 For further information and examples, Google is your friend :) 有关更多信息和示例,Google是您的朋友:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.