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);
    }
    
    
    
  • 相关阅读:
    第4章:kubectl命令行管理工具
    Docker_CICD笔记
    Harbor镜像仓库
    centos7 安装最新的 wiki confluence
    Centos7.5使用SSH密钥登录
    一篇文章带你搞懂 etcd 3.5 的核心特性
    腾讯云边缘容器 TKE Edge 国内首批通过边缘容器技术能力认证
    揭秘有状态服务上 Kubernetes 的核心技术
    腾讯云云原生混合云-TKE发行版
    kubernetes 降本增效标准指南|理解弹性,应用弹性
  • 原文地址:https://www.cnblogs.com/luowen/p/4756330.html
Copyright © 2011-2022 走看看