前端jQuery和原生js 实现下拉框select模糊搜索功能总结

本文总结了两种实现下拉框select模糊搜索的方法,一是使用jQuery,包括HTML结构和jQuery插件代码;二是通过原生JavaScript进行实现。
摘要由CSDN通过智能技术生成

方法一 : jQuery实现搜索

html 部分
<!DOCTYPE html>
<html lang="zh_cn">
  <head>
    <title>select.suggest</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!-- Bootstrap -->
    <script src="./jquery.js" type="text/javascript"></script>
  </head>
  <body>
    <h2>Hello, world!</h2>
    <div>
      <div>
        <div>
          <input
            id="testInput"
            name="testInput"
            type="text"
            placeholder="请选择区划"
          />
          <input type="hidden" name="cityRegionCode" id="cityRegionCode" />
        </div>
      </div>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <!-- Include all compiled plugins (below), or include individual files as needed -->
  </body>
  <script type="text/javascript">
    $("#testInput").val("省本级");
    $("#cityRegionCode").val("370001");
    var datasList = [
      {
     
        id: "1656",
        regionCode: "371723",
        regionName: "成武县",
        parentId: "1651",
        leaf: "1",
        status: null,
        parentRegionCode: null,
        parentRegionName: null,
      },
      {
     
        id: "1614",
        regionCode: "371325",
        regionName: "费县",
        parentId: "1605",
        leaf: "1",
        status: null,
        parentRegionCode: null,
        parentRegionName: null,
      },
      {
     
        id: "1529",
        regionCode: "370405",
        regionName: "台儿庄区",
        parentId: "1524",
        leaf: "1",
        status: null,
        parentRegionCode: null,
        parentRegionName: null,
      }
    ];
    // map遍历list
    console.log("---------map方法--------");
    var datas = datasList.map((item) => {
     
      return {
     
        ...item,
        text: item.regionName,
      };
    });
    var itemSelectFuntion = function () {
     
      console.log($("#testInput").val());
      console.log($("#cityRegionCode").val());
    };
    $.selectSuggest("testInput", datas, itemSelectFuntion);
  </script>
</html>
jquery.select.js部分
(function ($) {
   
  $.selectSuggest = function (target, data, itemSelectFunction) {
   
    // console.log('target', target, data, itemSelectFunction)
    var defaulOption = {
   
      suggestMaxHeight: '280px',//弹出框最大高度
      itemColor: '#000000',//默认字体颜色
      itemBackgroundColor: 'RGB(255,255,255)',//默认背景颜色
      itemOverColor: '#ffffff',//选中字体颜色
      itemOverBackgroundColor: '#1b84e3',//选中背景颜色
      itemPadding: 4,//item间距
      fontSize: 14,//默认字体大小
      alwaysShowALL: true //点击input是否展示所有可选项
    };
    var maxFontNumber = 0;//最大字数
    var currentItem;
    var suggestContainerId =
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值