商品查询案例

效果图

源码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box">
        <div class="top">
            <span>按照价格查询:</span>
            <span><input type="text" value="" class="xiao"></span>
            <span>-</span>
            <span><input type="text" value="" class="da"></span>
            <span><button class="sou">搜素</button></span>
            <span>按照商品名称查询:</span>
            <span><input type="text" value="" class="ming"></span>
            <span><button class="cha">查询</button></span>
        </div>
        <div class="botm">
            <table class="biao" width="800" border="1" cellspacing="0">
            </table>
        </div>
    </div>

    <script>
        // 获取元素
        var da = document.querySelector(".da");
        var xiao = document.querySelector(".xiao");
        var sou = document.querySelector(".sou");
        var ming = document.querySelector(".ming");
        var cha = document.querySelector(".cha");
        var biao = document.querySelector(".biao");


        var biaoo = [
            {
                title: "id",
                msg: "产品名称",
                price: '价格'
            }, {
                title: 1,
                msg: "小米手机",
                price: 9899
            }, {
                title: 2,
                msg: "oppoA52",
                price: 1299
            }, {
                title: 3,
                msg: "荣耀10",
                price: 1299
            }, {
                title: 4,
                msg: "华为p20",
                price: 2999
            }, {
                title: 5,
                msg: "华为p30",
                price: 3999
            }
        ]

        biaoo.forEach(function (v) {
            biao.innerHTML += `
                <tr>
                     <td>  ${v.title}</td>
                        <td> ${v.msg}</td>
                        <td> ${v.price}</td>
                </tr>    
            `
        })

        sou.addEventListener('click', function () {

            if (xiao.value == '' || da.value == '') {
                biao.innerHTML = ""
                biaoo.forEach(function (v) {
                    biao.innerHTML += `
                <tr>
                     <td>  ${v.title}</td>
                        <td> ${v.msg}</td>
                        <td> ${v.price}</td>
                </tr>    
            `
                })


            } else {
                biao.innerHTML = ""
                var rel = biaoo.filter(function (v) {
                    return xiao.value < v.price && da.value > v.price;

                })
                xiao.value = ''
                da.value = ''
                rel.forEach(function (v) {
                    biao.innerHTML += `
                <tr>
                     <td>${v.title}</td>
                        <td> ${v.msg}</td>
                        <td> ${v.price}</td>
                </tr>    
            `
                })
                console.log(rel);
            }
        })

        var rel =biaoo;
        cha.onclick = function () {
            biao.innerHTML = '';
            rel = rel.reduce(function (total, va, index, rel) {
                if (va.msg.includes(ming.value)) {
                    total.push(va);
                }
                return total;
            }, []);
            rel.forEach(function (v, i, a) {
                biao.innerHTML += `
                <tr>
                     <td>  ${v.title}</td>
                        <td> ${v.msg}</td>
                        <td> ${v.price}</td>
                </tr>    
            `
            });
            
        };


    </script>
</body>

</html>

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是一个使用Vue 3的setup函数编写的商品查询案例,该案例包含一个输入框和一个按钮,用户可以输入关键字并点击按钮进行搜索,搜索结果将展示在页面上。 ```html <template> <div> <input v-model="keyword" placeholder="请输入关键字"> <button @click="search">搜索</button> <ul> <li v-for="(item, index) in searchResult" :key="index">{{ item.name }} - {{ item.price }}</li> </ul> </div> </template> <script> import { ref } from 'vue' export default { setup() { const keyword = ref('') const searchResult = ref([]) const search = () => { // 模拟请求数据 const data = [ { name: '商品1', price: 100 }, { name: '商品2', price: 200 }, { name: '商品3', price: 300 }, ] // 根据关键字过滤数据 const result = data.filter(item => item.name.includes(keyword.value)) // 更新搜索结果 searchResult.value = result } return { keyword, searchResult, search, } }, } </script> ``` 在以上代码中,我们使用了Vue 3的`setup`函数来编写组件逻辑,其中使用了`ref`函数创建了两个响应式变量`keyword`和`searchResult`,分别表示搜索关键字和搜索结果。 `search`函数用于进行搜索,首先模拟了一个数据请求,然后根据关键字过滤数据,最后更新搜索结果。 在模板中,我们使用了`v-model`指令将输入框的值绑定到`keyword`变量上,使用`@click`指令监听按钮的点击事件并调用`search`函数进行搜索,使用`v-for`指令将搜索结果展示在页面上。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞不起来的飞机耶耶耶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值