1

我正在尝试从 PHP Jquery 食谱运行本教程,但第一个组合框没有填充国家数据并且它是空的!我在数据库中有 4 个表,我检查了它们,它们都很好!表是:Country、States、Towns 和 Towninfo

在我的 html 中,我有:

<html>
<head>
</head>
<body>
    <ul>
        <li>
            <strong>Country</strong>
            <select id="countryList">
                <option value="">select</option>
            </select>
        </li>
        <li>
            <strong>State</strong>
            <select id="stateList">
                <option value="">select</option>
            </select>
        </li>
        <li>
            <strong>Town</strong>
            <select id="townList">
                <option value="">select</option>
            </select>
        </li>
    </ul>
    <p id="information"></p>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function()
        {
       $('select').change(getList);
            getList();
            function getList()
            {
                var url, target;
                var id = $(this).attr('id');
                var selectedValue = $(this).val();
                switch (id)
                {
                    case 'countryList':
                        if(selectedValue == '') return;
                        url = 'results.php?find=states&id='+ selectedValue;
                        target = 'stateList';
                    break;

                    case 'stateList':
                        if($(this).val() == '') return;
                        url = 'results.php?find=towns&id='+ selectedValue;
                        target = 'townList';
                    break;

                    case 'townList':
                        if($(this).val() == '') return;
                        url = 'results.php?find=information&id='+ selectedValue;
                        target = 'information';
                    break;

                    default:
                        url = 'results.php?find=country';
                        target = 'countryList';
                }
                $.get(
                    url,
                    { },
                    function(data)
                    {
                        $('#'+target).html(data);
                    }
                )
            }
        });
    </script>
</body>
</html>

在我的 php 文件中:

 <?php
 $mysqli = new mysqli('localhost', 'root', '', 'chain');
 $find = $_GET['find'];
  switch ($find)
  {
case 'country':
$query = 'SELECT id, countryName FROM country';
break;

    case 'states':
$query = 'SELECT id, stateName FROM states WHERE countryId='.$_GET['id'];
break;

    case 'towns':
$query = 'SELECT id, townName FROM towns WHERE stateId='.$_GET['id'];
break;
case 'information':
    $query = 'SELECT id, description FROM towninfo WHERE townId='.$_GET['id'] .' LIMIT 1';
break;
    }
    if ($mysqli->query($query)) 
    {
$result = $mysqli->query($query);
if($find == 'information')
{
    if($result->num_rows > 0)
    {
        $row = $result->fetch_array();
        echo $row[1];
    }
    else
    {
        echo 'No Information found';
    }
}
else
{
     ?>
    <option value="">select</option>
     <?php          
    while($row = $result->fetch_array()) 
    {
      ?>        
        <option value="<?php echo $row[0]; ?>"><?php echo $row[1]; ?>         </option>     
      <?php         
    }
}
     }
     ?> 

根据书第一个组合框必须在页面加载后填充,但我不知道为什么它是空的!你能告诉我为什么会这样吗!

4

1 回答 1

0

调用:getList();

加载页面后将无法正常工作,因为它没有正确的值“this”上下文。

您可以尝试使用:

$('select').trigger("change");

而不是 getList(); 首次加载

或另一种尝试方式:

$(document).ready(function(){
    $('select').change(getList);
    getList.call($('select'));
    function getList(){...}
}

这应该设置正确的上下文。(但我不是 100% 确定它是否会起作用,因为没有尝试制作小提琴。)

于 2012-12-13T07:40:49.897 回答