zoukankan      html  css  js  c++  java
  • Knockoutjs 实现省市联动

    Knockoutjs 实现省市联动

    html code

    
    <!doctype>
    <html>
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta charset="utf-8">
            <link rel="stylesheet" type="text/css" href="bower_components/bootstrap/dist/css/bootstrap.css">
            <link rel="stylesheet" type="text/css" href="bower_components/bootstrap/dist/css/bootstrap-theme.css">
            <script src="bower_components/jquery/dist/jquery.js"></script>
            <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
            <script src="bower_components/knockoutjs/dist/knockout.debug.js"></script>
        </head>
        <body>
            <div class="container">
                <div class="row">
                    <div class="col-md-3" id="country">
                        <select class="form-control" data-bind="options: countryArray, optionsText: 'countryName', optionsValue: 'countryId', value: countryValue, optionsCaption: 'please select', event:{'change': getCity}"></select>
                    </div>
                    <div class="col-md-2" id="village">
                        <select class="form-control" data-bind="visible: countryValue, options: cityArray, optionsText: 'cityName', optionsCaption: 'Please select', optionsValue: 'cityId', value: 'cityValue'"></select>
                    </div>
                </div>
            </div>
        </body>
        <script>
            var countryModel = function(){
                var that = this;
    
                that.countryArray = ko.observableArray();
    
                that.countryValue = ko.observable();
    
                $.getJSON('http://localhost/demo.php', function(response) {//动态获取国家
                    that.countryArray(response);
                })
    
                that.getCity = function() {
                    var id = that.countryValue();
                    $.getJSON('http://localhost/demo.php', {id: id}, function(response){//动态获取城市
                        that.cityArray(response);
                    })
                }
    
                that.cityArray = ko.observableArray();
            }
    
            ko.applyBindings(new countryModel());
        </script>
    </html>
    
    

    javascript code

    <?php
    
    $country = array(
        array(
            'countryName' => 'china',
            'countryId' => 1,
        ),
        array(
            'countryName' => 'japan',
            'countryId' => 2,
        ),
        array(
            'countryName' => 'korean',
            'countryId' => 3,
        ),
    );
    
    if(isset($_GET['id']) && $_GET['id'] == '1')
    {
        $city = array(
            array(
                'cityName' => 'beijing',
                'cityId' => 1,
            ),
            array(
                'cityName' => 'shanghai',
                'cityId' => 2,
            ),
        );
    
        echo json_encode($city);
    }
    
    else if(isset($_GET['id']) && $_GET['id'] == 2)
    {
        $city = array(
            array(
                'cityName' => 'dongjing',
                'cityId' => 1,
            ),
            array(
                'cityName' => 'daban',
                'cityId' => 2,
            ),
        );
    
        echo json_encode($city);
    }
    
    else if(isset($_GET['id']) && $_GET['id'] == 3)
    {
        $city = array(
            array(
                'cityName' => '首尔',
                'cityId' => 1,
            ),
            array(
                'cityName' => 'ahha',
                'cityId' => 2,
            ),
        );
    
        echo json_encode($city);
    }
    
    else
    {
        echo json_encode($country);
    }
    
    
    
  • 相关阅读:
    ios中从相册:相机中获取图片信息
    ios中图片的绘画和截图
    UIPickView的简单介绍
    封装类的方式访问数据库(封装字符串、json)
    ajax用户名案例(重点)
    mysql中一对一,一对多,多对多关系
    会话控制:SESSION,COOKIE
    ajax简单案例:返回json型
    ajax同步,异步
    ajax简单案例:字符串返回类型
  • 原文地址:https://www.cnblogs.com/luowen/p/4756330.html
Copyright © 2011-2022 走看看