简体   繁体   English

如果在第一个下拉列表中选择了一个值(使用变量),则显示第二个下拉列表?

[英]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.

相关问题 如果在第一个下拉菜单中选择了某个值,如何显示第二个下拉菜单? - How to display a second dropdown menu if a certain value in the first dropdown is selected? 在下拉列表中显示预先选择的值 - display pre selected value in dropdown list 如何在国家/地区下拉列表中显示选定的值 - How to display selected value in country dropdown list 通过jQuery Ajax创建的下拉列表:如何在第一个下拉列表中的选定值的第二个下拉列表中获取相关项 - Dropdown created via jquery ajax:how to get related items in second dropdown of selected value in first dropdown 在不使用javascript的情况下在下拉列表中显示所选值 - Display the selected value in dropdown without using javascript 在第二个下拉列表中下拉选择的值 - Dropdown selected value in second dropdownlist 用与第一个下拉列表相同的值填充第二个下拉列表 - Fill second dropdown with the same value as the first dropdown 使用下拉列表中的选定值 - using selected value from dropdown list 获取所选项目的值并在第二个下拉列表中更改列表 - get value of selected item and change the list on second dropdown 如何根据第一个下拉框中选定的值在下拉框中获取第二个值 - How to get the second value in a dropdown box based on the selected one in the first dropdown box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM