【基础】发送请求(实例:通过百度接口jsonp请求下拉列表)

发送请求(实例:通过百度接口jsonp请求下拉列表)

  • css
<style>
    .gray_bg {
        background: #cccccc;
    }
</style>
  • html
<div id="app">
    <input type="text" v-model="textDefault" @keyup="vueJsonp()" @keydown="currentLineChange($event)">
    <ul>
        <li v-for="(item,index) in downList" :class="{gray_bg:index == currentLine}">{{item}}</li>
    </ul>
    <p v-show="downList.length == 0">暂无数据</p>
</div>
  • js
<script>
    // 初始化数据
    window.onload = function () {
        // vue
        new Vue({
            el: '#app',
            data: {
                textDefault: '', // 文本框默认值内容
                downList: [], // 下拉菜单内容
                currentLine: -1 // 当前行坐标
            },
            methods: {
                vueJsonp: function () {
                    this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su', {
                        params: {
                            wd: this.textDefault
                        },
                        jsonp: 'cb'
                    }).then(function (res) {
                        this.downList = res.body.s;
                    }, function (err) {
                        console.log(err);
                    })
                },
                /**
                 * 更新当前行数
                 */
                currentLineChange: function (event) {
                    if (event.keyCode == 40) {
                        // 当按下“↓”
                        if (this.currentLine <= this.downList.length) {
                            // 如果在结果集长度内 增加行数
                            this.currentLine++;
                        } else {
                            // 如果在结果集长度外 重置变量
                            this.currentLine = this.downList.length;
                        }
                    } else if (event.keyCode == 38) {
                        // 当按下“↑”
                        if (this.currentLine < 0) {
                            // 如果小于0 则取消显示
                            this.currentLine = 0;
                        } else {
                            this.currentLine--;
                        }
                    }

                }
            }
        });
    };
</script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,235评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,662评论 4 61
  • 先上几张我平时常做的泡菜成品给大家看看 泡盖菜 原料是鬼子姜和酸豇豆 调料有:白沙糖,味精,鸡精,辣椒油,花椒油(...
    劼希阅读 4,809评论 0 1
  • 自古以来,狗有狗的路,人有人道,但因狗路宽阔平坦,有些人走着走着也就走到了狗的路上。结果,前面是狗,身后是...
    非洲hello先生阅读 3,089评论 0 0
  • 无善无恶心之体,有善有恶意之动。知善知恶是良知,为善去恶是格物
    勇军阅读 1,319评论 0 0

友情链接更多精彩内容