简体   繁体   English

根据第一个下拉菜单选择填充第二个下拉菜单

[英]Populate 2nd Dropdown based on 1st Dropdown Choice

Basically, what I'm trying to accomplish is when someone clicks the first html dropdown id="FirstDD" , based on the option chosen the second dropdown id="SecDD" would populate with the fields that correlate to FirstDD dropdowns choice. 基本上,我要完成的工作是,当有人单击第一个html下拉列表id="FirstDD" ,根据选择的option ,第二个下拉列表id="SecDD"将填充与FirstDD下拉列表选项相关的字段。 Triggering this all is ajax to do the initial call and php to handle from there in subcategories.php . 触发所有这些操作就是ajax进行初始调用,然后从subcategories.php php处理。

Currently, what is happening is: i selected the first dropdown, but nothing gets populated in SecDD 目前,发生的事情是:我选择了第一个下拉列表,但是SecDD没有填充任何SecDD

JS/AJAX: JS / AJAX:

    <script type="text/javascript">
    $(document).ready(function(){
    $("#FirstDD").change(function(){
        $('#SecDD').load('inc/subcategories.php?scatID='+this.value);
        });
    });
    </script>

HTML 的HTML

   <select style="width:300px;" id="FirstDD" name="userListingCategory">
                    <!--onchange="$('#SecDD').load('inc/subcategories.php?scatID='+this.value);"-->
                          <option  disabled="disabled">Category...</option>
                          <?php while($row = $sth2->fetch(PDO::FETCH_ASSOC))
                          {echo "<option value=". $categoryID . ">" .$row['catName']."</option>";}
                        unset($sth2);
                        ?>

                    </select> 
                   <select style="width:340px;" id="SecDD" name="userListingSCategory" style="display:none">
                    <?php require_once('inc/subcategories.php'); ?>
                    </div>
                    </select> 

Subcategories.php Subcategories.php

<?php require_once('db/dbc.php');
#GET SELECT sub-category names
$pdo3 = new PDO($h1, $u, $p);
$pdo3->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth3 = $pdo3->prepare("
SELECT scatID, scatName
FROM Category C, SubCategory SC
WHERE C.catID = SC.catID
;");
$sth3->execute(array());
?>

<option  disabled="disabled">Sub-Category...</option>
<?php
#Get subcats    
while($row = $sth3->fetch(PDO::FETCH_ASSOC))
{echo "<option value=". $row['scatID'] . ">" .$row['scatName']."</option>";}
unset($sth3);
?>

Diagnose the issue: 诊断问题:

  • Get the php to echo this.value to see if the contents of that are what you're expecting. 获取php来回显this.value,以查看其内容是否符合您的期望。
  • Run the SQL query with the contents you're expecting and see if it returns the data you're expecting. 使用您期望的内容运行SQL查询,并查看它是否返回您期望的数据。

If both of those work fine each time, then something's not triggering, but the code seems sound from here. 如果这两种方法每次都能正常工作,则说明没有什么触发条件,但是代码似乎从这里发出。 After that, I would try putting echo statements in between your lines and see where it stops. 之后,我将尝试将echo语句放在行之间,并查看其停止位置。

暂无
暂无

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

相关问题 根据PHP中选择的第一个下拉列表填充第二个下拉列表 - Populate 2nd dropdown based on 1st dropdown selected in PHP 基于第一个下拉列表的动态第二个下拉列表 - Dynamic 2nd Dropdown-list based on the 1st Dropdown 如何根据从第一个下拉菜单自动更改的第二个下拉菜单更改第二个文本框的值? - How to change the 2nd textbox value based on the 2nd dropdown that is automatically changed from 1st dropdown? 从第一个填充第二个列表框 - populate 2nd listbox from 1st 如何关联2个下拉列表并在相对于1st的第二个下拉列表中加载数据 - how to relate 2 dropdown and load data in 2nd dropdown in relation to 1st 根据使用 jquery 和 php 在第一个下拉列表中选择的元素,在第二个下拉列表中添加元素 - add elements in 2nd dropdown list depending upon the element selected in 1st dropdown using jquery and php 如何改变 <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 如何在第1列和第2列的交叉点上填充第3列结果,这些列都来自用户在下拉列表中选择的同一个表? - How can I populate a 3rd column result on intersections of 1st and 2nd columns all from the same table that are selected by the user on dropdown? 通过在codeigniter php中使用ajax,根据第一个下拉结果填充第二个下拉列表 - populate 2nd drop down based on 1st drop down result by using ajax in codeigniter php 使用 API JSON 根据第一个下拉列表填充第二个下拉列表 - Populate 2nd drop-down list based on 1st drop-down using API JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM