首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jquery、php、mysql下拉菜单

问使用jquery、php、mysql下拉菜单
EN

Stack Overflow用户
提问于 2014-04-11 17:15:12
回答 1查看 939关注 0票数 0

我对编程相当陌生,所以如果你能包括解释,这样我就可以学到了,我会非常感激的。

好的,我正在从sql表中做一个下拉菜单,我使用的是php和Jquery。到目前为止,我已经得到了我的第一个子菜单,也就是从我的国家菜单中填充的州。现在,我对如何让我的城市菜单从我的州菜单中填充感到困惑。

这是我的主php文件!

代码语言:javascript
复制
 <script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#flip").click(function() {
            jQuery("#panel").slideToggle("slow");
        });

        jQuery("#country").change(function() {
            //jQuery("#address").val(jQuery("#courseid :selected").val());
            var querystr = 'countryid='+jQuery('#country :selected').val();
            jQuery.post("<?php echo plugins_url(); ?>/CountryStateCity Drop Down/ajax.php", querystr, function(data){
                if(data.errorcode == 0){
                    jQuery('#statecbo').html(data.chtml)
                    //jQuery('#citydescr').append('<textarea name="citydescr" id="citydescr" cols="80" rows="3" maxlength="500"></textarea>')
                }else{
                    jQuery('#statecbo').html(data.chtml)
                }
            }, "json");
        });
    });
</script>

<html>
    <head>
        <title>Dynamic Drop Down Menu</title>

    </head>
    <body>
        <div class="wrap">
        <h5> Country</h5>
         <select id="country" name="country" required>
             <option value="">--Select Country--</option>
            <?php
            $sql=mysql_query("SELECT * from country order by name");
            while ($row=mysql_fetch_array($sql)) {
                $countryID=$row['IDCountry'];
                $countryname=$row['name'];
                echo "<option value='$countryID'>$countryname</option>";
             }
             ?>
             </select>
         </div>
        <h5>State</h5>
        <div class="wrap"  id="statecbo">



        </div>

        <div class="wrap">
             <h5>City</h5>

    </div>
    </body>
   </html>

这是我的ajax.php文件

代码语言:javascript
复制
$country_id = isset($_POST['countryid']) ? $_POST['countryid'] : 0;
if ($country_id <> 0) {
$errorcode = 0;
$strmsg = "";
$sql="SELECT * from state WHERE IDCountry = ". $country_id . " ORDER BY name;";
$result=mysql_query($sql);
$cont=mysql_num_rows($result);
if(mysql_num_rows($result)){
    $chtml = '<select name="states" id="states"><option value="0">--Select State--     </option>';
    while($row = mysql_fetch_array($result)){
        $chtml .= '<option value="'.$row['IDState'].'">'.$row['name'].'</option>';
    }
    $chtml .= '</select>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$chtml));
}else{
    $errorcode = 1;
    $strmsg = '<font style="color:#F00;">No States available</font>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$strmsg));
}


}

因此,我的下一步将是添加一个城市菜单,该菜单由我刚刚从乡村菜单中填充的状态菜单填充。抱歉,如果这让人困惑的话。谢谢!

基于off jeroen的反应,这里是我添加的,试图得到城市下拉菜单。

我的主php文件--

代码语言:javascript
复制
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#flip").click(function() {
            jQuery("#panel").slideToggle("slow");
        });

        jQuery("#country").change(function() {

            var querystr = 'countryid='+jQuery('#country :selected').val();
            jQuery.post("<?php echo plugins_url(); ?>/CountryStateCity Drop Down/ajax.php", querystr, function(data){
                if(data.errorcode == 0){
                    jQuery('#statecbo').html(data.chtml)

                }else{
                    jQuery('#statecbo').html(data.chtml)
                }
            }, "json");
        });
        jquery(".wrap").on('change', '#states',function() {
            var querystr = 'stateid=' +jQuery('#states :selected').val();
            jquery.post("<?php echo plugins_url(); ?>/CountryStateCity Drop Down/ajax.php", querystr, function(data) {
                if(data.errorcode ==0){
                    jQuery('#citycbo').html(data.chtml)
                }else{
                    jQuery('#citycbo').html(data.chtml)
                }
            }, "json");
        });
    });
</script>

还有我的ajax.php文件

代码语言:javascript
复制
$country_id = isset($_POST['countryid']) ? $_POST['countryid'] : 0;
if ($country_id <> 0) {
$errorcode = 0;
$strmsg = "";
$sql="SELECT * from state WHERE IDCountry = ". $country_id . " ORDER BY name;";
$result=mysql_query($sql);
$cont=mysql_num_rows($result);
if(mysql_num_rows($result)){
    $chtml = '<select name="states" id="states"><option value="0">--Select State--</option>';
    while($row = mysql_fetch_array($result)){
        $chtml .= '<option value="'.$row['IDState'].'">'.$row['name'].'</option>';
    }
    $chtml .= '</select>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$chtml));
}else{
    $errorcode = 1;
    $strmsg = '<font style="color:#F00;">No States available</font>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$strmsg));
}


}

$state_id = isset($_POST['IDState']) ? $_POST['IDState'] : 0;
if ($state_id <> 0) {
$errorcode = 0;
$strmsg = "";
$sql="SELECT * from state WHERE IDState = ". $state_id . " ORDER BY name;";
$result=mysql_query($sql);
$cont=mysql_num_rows($result);
if(mysql_num_rows($result)){
    $chtml = '<select name="city" id="city"><option value="0">--Select city--  </option>';
    while($row = mysql_fetch_array($result)){
        $chtml .= '<option value="'.$row['IDCity'].'">'.$row['name'].'</option>';
    }
    $chtml .= '</select>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$chtml));
}else{
    $errorcode = 1;
    $strmsg = '<font style="color:#F00;">No city available</font>';
    echo json_encode(array("errorcode"=>$errorcode,"chtml"=>$strmsg));
}


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-11 17:21:52

基本上,你对国家/州所做的事情都是一样的。

但是,您使用的是jQuery change()函数,这样就不能处理注册该函数时页面上没有的元素。

您可以通过使用事件委托来解决这个问题:

代码语言:javascript
复制
jQuery(".wrap").on('change', '#states', function() {
  // do your stuff
}

我使用了.wrap元素,因为它是包装表单元素的元素,但您也可以使用document。

当然,对于你已经拥有的东西,你可以使用同样的方法,只需改变:

代码语言:javascript
复制
jQuery("#country").change(function() {

至:

代码语言:javascript
复制
jQuery(".wrap").on('change', '#country', function() {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23018738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档