doumu5934 2014-03-14 14:46
浏览 44
已采纳

根据选择填充额外的下拉列表

first of all this code works like a charm, my question is to add a feature. I would like to know how can I populate next drop down based on selection. so for example I select first stock 'US500' and it would show me prices on second drop-down. how can I do this?

data.php

<?php
if(!empty($_GET['getlist']))
{
    $list = $_GET['getlist'];
    $qry='';
    switch($list)
    {
        case 'tracks':
        {
            $qry = "SELECT TABLE_NAME as allStocks FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'db_stocks' AND TABLE_ROWS > 100";
            break;
        }
    }
    if(empty($qry)){ echo "invalid params! "; exit; }

    $dbconn = mysql_connect('localhost','user','pass') 
            or die("DB login failed!");

    mysql_select_db('db_nadex_stocks', $dbconn) 
            or die("Database does not exist! ".mysql_error($dbconn));

    $result = mysql_query($qry,$dbconn)
            or die("Query $qry failed! ".mysql_error($dbconn));

    $rows = array();

    while($rec = mysql_fetch_assoc($result)) 
    {
        $rows[] = $rec;
    }
    mysql_free_result($result);
    mysql_close($dbconn);

    echo json_encode($rows);
}
?>

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>List Sample 1</title>
<style>
body{ font-family : Arial, Helvetica, sans-serif; font-size : 1em;}
h1 {font-size : 18pt;   color : #000066;}
h2 {font-size : 14pt;   color : #000066;}
a:hover{text-decoration : none;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
    function loadlist(selobj,url,nameattr) {
        $(selobj).empty();
        $.getJSON(url,{},function(data)
        {
            $.each(data, function(i,obj)
            {
                $(selobj).append($('<option></option>').val(obj[nameattr]).html(obj[nameattr]));
            });
        });
    }

    $(function() { 
       loadlist($('select#sel1').get(0), 'data.php?getlist=tracks','allStocks');
       loadlist($('select#sel2').get(0), 'data.php?getlist=tracks','allStocks');
       loadlist($('select#sel3').get(0), 'data.php?getlist=tracks','allStocks');
    });
</script>
</head>

<body>
<h1>Dynamically lists: multiple lists</h1>
<p>
dfgdgf 1:<select name='sdfsdf' id='sel1' size='1'></select>
</p>
<p>
cvbcvb 2:<select name='sdfsdf' id='sel2' size='1'></select>
</p>
<p>
cvbcv 3:<select name='dfgdfg' id='sel3' size='1'></select>
</p>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • douzhuozhu9544 2014-03-14 15:09
    关注

    You will need to add in an onChange to the first dropdown and then send the selected index of the first dropdown to the same function. Something like this in the onChange.

    loadlist($('select#sel2').get(0), 'data.php?getlist=prices&stock_id=' + $(this).val(),'allStocks');
    

    And then add into the switch statement a check for 'prices' and query for the prices using the stock_id

    $_GET['stock_id']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面