根据选择的州显示马来西亚城市 [英] Show Malaysia cities based on states chosen

查看:131
本文介绍了根据选择的州显示马来西亚城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个马来西亚州和城市下拉列表。




  • 这是我的初始页面:(test3.php) / li>





  • 当我选择KL时,城市的预期输出如下所示:



>




  • 当我选择雪兰莪州时,我的城市预期输出如下:






  • 状态数据json ($ stateJsonObject)

      => stdClass Object([stateId] => s1 [stateName] =>吉隆坡)
    [1] => stdClass Object([stateId] => s2 [stateName] => )


  • 城市资料json ($ cityJsonObject) >

      Array([0] => stdClass Object([cityId] => c1 [cityName] => Kajang [cityStateId] 
    => s2)[1] => stdClass Object([cityId] => c2 [cityName] => Seputeh
    [cityStateId] => s1)[2] => stdClass Object([cityId] => c3 [cityName] => Shah
    Alam [cityStateId] => s2)[3] => stdClass Object([cityId] => c4 [cityName] =>
    Klang [cityStateId] => s2)[4] =& stdClass Object([cityId] => c5 [cityName] =>
    Kepong [cityStateId] => s1))

    li>

  • 代码(test3.php)

     < html& 
    < head>
    < script type =text / javascript>
    function showCity()
    {
    //从下拉列表中的状态ID
    var stateId = state.options [state.selectedIndex] .value;

    // CODE HERE
    }
    < / script>
    < / head>

    < body>
    < form action =test3.phpmethod =post>
    状态:
    < select name =stateid =stateonchange =showCity();>
    < option value =>选择一个< / option>
    <?php
    for($ i = 0; $ i {
    echo'< option value ='。 $ stateJsonObject [$ i] - > stateId。'>';
    echo $ stateJsonObject [$ i] - > stateName;
    echo'< / option>';
    }
    ?>
    < / select>

    < br />

    城市:
    < select name =cityid =city>
    < option value =>选择一个< / option>
    < / select>
    < / form>
    < / body>
    < / html>基于上述参考,以下是我的问题:

    b(1)如何比较此处部分中的js和php(json)之间的状态ID

    (2)如何根据我在此处
    部分选择的状态显示城市下拉列表?



解决方案

如我所见,您有两种选择:



1。)在一个大JSON中加载整个对象,并在Javascript中准备好它 - 然后用JS操作select。



2。)如果第一个没有这么好(biiiiig数据),lazy加载选择 - 再次使用AJAX - 并渲染选择与JS。



JS为所有可能的操作,我认为它是最简单的:)



ps:当编写方法渲染选择 - 思考所有可能的情况,并有复位功能他们 - 它会帮助你在结束:)


I want to do a Malaysia states and cities drop down list.

  • Here is my initial page: (test3.php)

  • When I select KL, my expectation output of the city shown as following:

  • When I select Selangor, my expectation output of the city shown as following:

  • State data json ($stateJsonObject)

    Array ( [0] => stdClass Object ( [stateId] => s1 [stateName] => Kuala Lumpur) 
    [1] => stdClass Object ( [stateId] => s2 [stateName] => Selangor))
    

  • City data json ($cityJsonObject)

    Array ( [0] => stdClass Object ( [cityId] => c1 [cityName] => Kajang [cityStateId] 
    => s2 ) [1] => stdClass Object ( [cityId] => c2 [cityName] => Seputeh 
    [cityStateId] => s1 ) [2] => stdClass Object ( [cityId] => c3 [cityName] => Shah 
    Alam [cityStateId] => s2 ) [3] => stdClass Object ( [cityId] => c4 [cityName] => 
    Klang [cityStateId] => s2 ) [4] => stdClass Object ( [cityId] => c5 [cityName] => 
    Kepong [cityStateId] => s1 ))
    

  • Code (test3.php)

    <html>
    <head>
    <script type="text/javascript">
        function showCity()
        {
            //state id from drop down list
            var stateId = state.options[state.selectedIndex].value;
    
            //CODE HERE
        }
    </script>
    </head>
    
    <body>
        <form action="test3.php" method="post">
        State:
        <select name="state" id="state" onchange="showCity();">
            <option value ="">select one</option>
            <?php
                for($i = 0; $i < count($stateJsonObject); $i++)
                {
                    echo '<option value = '.$stateJsonObject[$i] -> stateId.'>';
                    echo $stateJsonObject[$i] -> stateName;
                    echo '</option>';
                }
            ?>
        </select>
    
        <br />
    
        City:
        <select name="city" id="city">
            <option value ="">select one</option>
        </select>
    </form>
    </body>
    </html>
    

  • Based on the above reference, below is my questions:
    (1) How do I compare the state id between js and php (json) on CODE HERE section?
    (2) How to display the city drop down list based on state that I choose on CODE HERE section?

解决方案

As I can see it you have two options:

1.) load whole object in one "big" JSON and have it ready in Javascript - then just manipulate select's with JS.

2.) if number one is not performing so well (biiiiig data) "lazy" load selects - again with AJAX - and render selects with JS.

So - use JS for all manipulation where possibe and I think it's easiest :)

p.s.: When writing methods for rendering selects - think well about all possible scenarios and also have a reset function for them - it will help you at the end :)

这篇关于根据选择的州显示马来西亚城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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