我如何通过从另一个下拉列表中选择值来填充下拉列表? [英] How can i populate a dropdown list by selecting the value from another dropdown list?

查看:91
本文介绍了我如何通过从另一个下拉列表中选择值来填充下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过从mysql数据库获取两个下拉列表的记录来构建两个下拉列表(例如州和城市),并试图构建一个工具,在从第一个下拉列表中选择任何值(即任何状态)的同时,在第二次下拉(在城市中)时,只有那些在第一次下拉选择的值(州)下的值(城市)应该是可见的。



以下是我的代码:

 < tr> 
< td id ='hed'>< span style =font-family:verdana,geneva,sans-serif>状态< /状态>< / td>
< td>
<?php
$ dbcon = mysql_connect(@ ip,@ username,@ password);

if($ dbcon)
{
mysql_select_db(@ database,$ dbcon);
}
else
{
die('连接数据库的错误');
}

$ qry =select @value(state)from @tablename;
$ result = mysql_query($ qry)或die(mysql_error());

dropdown =< select name ='@ valuename'id ='officeItemList'style ='cursor:pointer; cursor:hand;'>;
while($ row = mysql_fetch_array($ result))
{
$ dropdown。=\r\\\
< option value ='{$ row ['@ value']} '> {$ row ['@ value']}< / option>;
}
$ dropdown。=\r\\\
< / select>;
echo $ dropdown;
mysql_close($ dbcon);
?>
< / td>
< / tr>

< tr>
< td id ='hed'>< span style =font-family:verdana,geneva,sans-serif>城市< / span>< / td>
< td colspan =1>
<?php
$ dbcon = mysql_connect(@ ip,@ username,@ password);

if($ dbcon)
{
mysql_select_db(@ database,$ dbcon);
}
else
{
die('连接数据库的错误');
}

$ qry =select value2(city)from @tablename where;
$ result = mysql_query($ qry)或die(mysql_error());

dropdown =< select name ='@ value2'id ='officeItemList'style ='cursor:pointer; cursor:hand;'>;
while($ row = mysql_fetch_array($ result))
{

$ dropdown。=\r\\\
< option value ='{$ row ['@ value2']}'> {$ row ['@ value2']}< / option>;
}
$ dropdown。=\r\\\
< / select>;
echo $ dropdown;
mysql_close($ dbcon);
?>


< / td>
< / tr>


解决方案

您可以使用AJAX获取选定的城市州。例如:

  $(select#state)。change(
function(){
var state = $(this).val();
$ .ajax({
类型:GET,
url:get_cities.php /?state =+ state,
//根据所选状态编写一个查询并返回OPTION的
的HTML成功:function(cities){
$(select#cities)。html(cities);
}
});
}
);

您也可以返回一个json对象(在这种情况下,不要忘记添加 dataType:json),并在客户端转换为HTML,即在 success 函数中。 b $ b

I have build two drop downs (like state and city) by fetching the records of both drop downs from mysql database and am trying to build the tool in which, while selecting any value (i.e. any state) from first drop down, at that time in second drop down (in city) only those values (cities) under that value (state) selected in first drop down should be visible.

Here's my code:

<tr>    
        <td id='hed'><span style="font-family:verdana,geneva,sans-  serif">State</state></td>
        <td>
        <?php 
        $dbcon = mysql_connect("@ip","@username","@password");

        if($dbcon)
        {
            mysql_select_db("@database", $dbcon);
        }
        else
        {
            die('error connecting to the database');
        }

        $qry = "select @value(state) from @tablename  ";
        $result = mysql_query($qry) or die(mysql_error());

        $dropdown = "<select name='@valuename' id='officeItemList' style='cursor:pointer;cursor:hand;'>";
        while($row = mysql_fetch_array($result))
        {           
            $dropdown .= "\r\n<option value='{$row['@value']}' > {$row['@value']} </option>";
        }
        $dropdown .= "\r\n</select>"; 
        echo $dropdown;
        mysql_close($dbcon);
        ?>
        </td> 
    </tr>

        <tr>
        <td id='hed'><span style="font-family:verdana,geneva,sans-serif">City</span></td>
        <td colspan="1"> 
        <?php 
        $dbcon = mysql_connect("@ip","@username","@password");

        if($dbcon)  
        {
            mysql_select_db("@database", $dbcon);
        }  
        else
        {
            die('error connecting to the database');
        }  

        $qry = "select value2(city) from @tablename where ";
        $result = mysql_query($qry) or die(mysql_error()); 

        $dropdown = "<select name='@value2' id='officeItemList' style='cursor:pointer;cursor:hand;'>";
        while($row = mysql_fetch_array($result)) 
        {

            $dropdown .= "\r\n<option value='{$row['@value2']}' > {$row['@value2']} </option>";
        }
        $dropdown .= "\r\n</select>"; 
        echo $dropdown;
        mysql_close($dbcon);
        ?>      


        </td>
    </tr>

解决方案

You can use AJAX to fetch the cities for the selected state. Something like:

$("select#state").change(
function(){
   var state = $(this).val();
   $.ajax({
  type: "GET",
  url: "get_cities.php/?state=" + state, 
// write a query according to the state selected and return the HTML for the OPTION's
  success: function(cities){
    $("select#cities").html(cities);
   }
}); 
}
);

You can also return a json object (in which case don't forget to add dataType:"json") and make the transition to HTML in the client-side, i.e inside the success function

这篇关于我如何通过从另一个下拉列表中选择值来填充下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆