duanmu2015 2011-10-22 15:09
浏览 14

获取与首次下拉选择相关的值

<select style="width:300px;" id="FirstDD" name="userListingCategory" onchange="FillSelectTwo(this.options[this.selectedIndex].value)">
                      <option  disabled="disabled">Category...</option>
                      <?php while($row = $sth2->fetch(PDO::FETCH_ASSOC))
                      {echo "<option value=". $row['catID'] . ">" .$row['catName']."</option>";}
                    unset($sth2);
            $selectedOptionInCategory = $row['catID'];
                    ?>

                </select> 
                 <select style="width:340px;" id="SecDD" name="userListingSCategory">
                   <option  disabled="disabled">Sub-Category...</option>      
                <?php while($row = $sth3->fetch(PDO::FETCH_ASSOC))
                      {echo "<option value=". $row['scatID'] . ">" .$row['scatName']."</option>";}
                    unset($sth3);
                 ?>

When a 'category' is selected like: img1

I want the subcategories associated with that first category chosen to show up.

I'm running the following SQL:

    SELECT scatID, scatName 
FROM Category C, SubCategory SC
WHERE C.catID = SC.catID 
AND C.catID = '$selectedOptionInCategory'

JS:

function FillSelectTwo(id) {  //what is this id for?  
    $.ajax({
        url: 'index.php',
        dataType: 'text',
        success: function(data) {           
         $('#SecDD').find('option').remove().end().append(data);
        },
        error:function(jxhr){
         console.log(jxhr.responseText);        
        }
    });
}

Problem I get When I test this code out, I selected a category like Baby, then the subcategory box clears the first 'disabled' option value, but then its an empty set from there and I can't selected any of the values that the SQL would return upon selecting a category.

What it looks like: img3img4

  • 写回答

2条回答 默认 最新

  • dshdsh2016 2011-10-22 15:26
    关注

    javascript ssems to be fine but i doubt if the ajax call is going out in the first place

    see in the firebug or developer tools(for chrome) to confirm that the ajax call is made to index.php

    also remove the options in the success callback so if the ajax fails the select remains intact and make an error callback to trace errors

     function FillSelectTwo(id) {  //what is this id for?  
        $.ajax({
            url: 'index.php',
            dataType: 'text',
            success: function(data) {           
             $('#SecDD').find('option').remove().end().append(data);
            },
            error:function(jxhr){
             console.log(jxhr.responseText);        
            }
        });
    }
    

    P.S. the is a ' missing at the end of your url may be that is causing the problem

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程