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);
    }
    
    
    
  • 相关阅读:
    Java基础学习07--内部类与常用类
    Java基础学习06--集合
    leetcode124
    Python深度学习笔记09--使用Keras建立循环神经网络
    Python深度学习笔记08--处理文本数据的常用方法
    详解DNS域名解析系统(域名、域名服务器[根、顶级、授权/权限、本地]、域名解析过程[递归与迭代])
    计算机网络之应用层概述(C/S模型与p2p模型)
    计算机网络传输层之TCP拥塞控制(慢开始与拥塞避免、快重传和快恢复)
    计算机网络传输层之TCP流量控制
    计算机网络传输层之TCP可靠传输
  • 原文地址:https://www.cnblogs.com/luowen/p/4756330.html
Copyright © 2011-2022 走看看