简体   繁体   English

如何改变 <option>第3个<select>基于第二次变化<select> ,其中第二个是通过使用jquery ajax更改第一个下拉列表的值来绘制的

[英]How to change <option> of a 3rd <select> based on change in 2nd <select>, where 2nd is drawn by changing value of 1st dropdown using jquery ajax

I got a little weird situation in my project (for a school management) and could not find a suitable solution for it on internet. 我的项目(学校管理层)有点奇怪的情况,在互联网上找不到合适的解决方案。 here is the problem, I have three <select> element, the first is to select class, second one is to select section in that class and third one is to select course from that section. 这是问题,我有三个<select>元素,第一个是选择类,第二个是选择该类中的部分,第三个是从该部分选择课程。 the code is as following 代码如下

<select id="class">
    <option value="class1">Class 1</option>
    <option value="class2">Class 2</option>
</select>


<select id="section">
    <!--sections will be drawn here from database using ajax, based on class selected in 1st dropdown-->
</select>


<select id="course">
    <!--when a section is selected in 2nd dropdown, courses of that section will be drawn here from database using ajax-->
</select>

my ajax code to draw #section when change #class is following, it simply take the selected class and send it to draw_sections.php, where I used $_GET[] to get its value and used it in a query to select sections of that class from database 更改#class后我的ajax代码绘制#section,它只需要选择的类并将其发送到draw_sections.php,我在其中使用$ _GET []获取其值并在查询中使用它来选择其中的部分数据库中的类

$("#class").change(function(){
   var class = $(this).val();
   $.ajax({
       url: "draw_sections.php?class="+class+"", success: function(sections) {
         $("#section").html(sections);
       } 
    });
});

Till this point, it works fine and draws the sections of selected class. 直到这一点,它工作正常并绘制选定类的部分。 Now i want to draw courses in 3rd drop down <select> when a section is selected in 2nd <select> . 现在我想在第二个<select>选择一个部分时在第三个下拉列表<select>绘制课程。 here is ajax my ajax code 这是ajax我的ajax代码

$("#section").change(function(){
       var section = $(this).val();
       $.ajax({
           url: "draw_courses.php?section="+section+"", success: function(courses) {
             $("#course").html(courses);
           } 
        });
  });

this function was intended to take section (like it did in class) to draw_course.php and select courses of that section from database and then draw it in 3rd drop down (as it did in first drop down ie class) but the problem is as the sections are drawn through ajax, selecting a section does not affect the course drop down. 这个函数的目的是将section(就像在课堂上一样)绘制到draw_course.php并从数据库中选择该部分的课程,然后在第3个下拉中绘制它(就像它在第一个下拉即类中所做的那样)但问题是这些部分是通过ajax绘制的,选择一个部分不会影响课程下拉。 also the sections do not exist in source code. 这些部分在源代码中也不存在。 as I am beginner with ajax, so could not explain my problem better than this. 因为我是ajax的初学者,所以无法比这更好地解释我的问题。

Try jQuery .on 试试jQuery .on

  $("#section").on('change',function(){
      //code here
    });

YOu need to use either delegate or on since the select boxes are build dynamically 你需要使用delegateon因为select框是动态构建的

$("body").delegate('#section','change',function(){
  //code
});

Or 要么

$("#section").on('change',function(){
//code
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据从第一个下拉菜单自动更改的第二个下拉菜单更改第二个文本框的值? - How to change the 2nd textbox value based on the 2nd dropdown that is automatically changed from 1st dropdown? 如何在选项标签 select 下拉列表 1st 2nd 3rd 4th 5th 中添加上标。 1ST 我想要 S 大写字母,但我试过 s 将转换为小写 - How to add superscript in option tag select dropdown 1st 2nd 3rd 4th 5th. 1ST I want S captial letters but i tried s will converted to small case 填充在第一个选择中选择的选项的第二个选择 deoending - Populate 2nd select deoending of option choosen in 1st select 使用ajax和PHP基于第一个选择框填充第二个选择框 - populating a 2nd select box based off the 1st one using ajax and PHP 想要通过 ajax.1st 下拉值获取 3level 下拉值的值,但是如何在不使用 session 的情况下获取 2nd 和 3rd - want to fetch value of 3level dropdown through ajax.1st dropdown value is fetched but how to fetch 2nd and 3rd without using session 根据在第一个选择框中选择的值,从第二个选择框中的数据库表中选择选项 - Bring the Options from database table in 2nd Select box based on the value selected in 1st Select Box 使用第一个 select 框值从数据库中触发/过滤第二个 select 框的数据 - Using 1st select box value to trigger/filter the data of 2nd select box from database 使用碳查找一周中的每一天 [1st,2nd,3rd] 对于接下来的 15 天 - Find every [ 1st,2nd,3rd ] day of week using carbon For next 15 day 基于第一个下拉列表的动态第二个下拉列表 - Dynamic 2nd Dropdown-list based on the 1st Dropdown 根据第一个下拉菜单选择填充第二个下拉菜单 - Populate 2nd Dropdown based on 1st Dropdown Choice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM