Promise简易下拉联动

127 阅读1分钟
guojia.txt:
中国 韩国 美国 日本 意大利


sheng.txt:
江苏 上海 安徽 浙江


shi.txt:
南京 南通 徐州 扬州
<div>
        <select name="" id="sel1">
            <option value="国家">国家</option>
        </select>
        <select name="" id="sel2">
            <option value="省级" id="se1">省级</option>
        </select>
        <select name="" id="sel3">
            <option value="市级" id="se2">市级</option>
        </select>
    </div>
<script src="./jquery-3.6.0.js"></script>
<script>
    let arr = []
    let arr1 = []
    let arr2 = []
    new Promise(
        function (a, b) {
            $.ajax({
                url: 'guojia.txt',
                success: function (res) {
                    a(res)
                }
            })
        }
    )
        .then(val => {
            arr = val.split(' ')
            console.log(arr);
            for (var i in arr) {
                $('#sel1').append(
                    `
            <option value="${arr[i]}">${arr[i]}</option>
            `
                )
            }
            $('#sel1').change(function () {
                if ($('#sel1').val() == '中国') {
                    new Promise(
                        function (a, b) {
                            $.ajax({
                                url: 'sheng.txt',
                                success: function (res) {
                                    a(res)
                                }
                            })
                        }
                    ).then(val => {
                        arr2 = val.split(' ')
                        console.log(arr2);
                        for (var i in arr2) {
                            $('#sel2').append(
                                `
            <option value="${arr2[i]}">${arr2[i]}</option>
            `
                            )
                        }
                        $('#sel2').change(function () {
                            if ($('#sel2').val() == '江苏') {
                                
                                new Promise(
                                    function (a, b) {
                                        $.ajax({
                                            url: 'shi.txt',
                                            success: function (res) {
                                                a(res)
                                            }
                                        })
                                    }
                                )
                                    .then(val => {
                                        arr3 = val.split(' ')
                                        console.log(arr3);
                                        for (var i in arr3) {
                                            $('#sel3').append(
                                                `
            <option value="${arr3[i]}">${arr3[i]}</option>
            `
                                            )
                                        }
                                    })
                            } else {
                                $('#sel3 ').html(
                                    `
            <option value="市级">市级</option>
            `
                                )
                            }
                        })
                    })
                }
                else {
                    $('#sel2').html(
                        `
            <option value="省级">省级</option>
            `
                    )

                    $('#sel3 ').html(
                        `
            <option value="市级">市级</option>
            `
                    )

                }
            })

        })

</script>