搜索下拉框

当选择下拉框有很多选项时是否觉得很难选择?
若然经过查询筛选出有用的选项是否觉得方便很多?以下代码可能对你用。---[ jscrpit for IE6.0 ]

******js部份,把它存为"findInSelect.js"文件方便调用]******

//===gotCode for findInSelect.js 2006-1-9===
//===搜索下拉框===
document.write('<span id="draw_findInSelect"></span>')

//--建立搜索输入框
function drawInput_findInSelect(){
 oHtml = ''+
   '<div style="position:absolute;top:100;left:100" id="SS_search_Div">'+
    '<div style="'+
     ';position:absolute' +
     ';z-index:2'   +
     ';width:100%'   +
    '">/n' +
     '<input name="SS_search" type="text" style="width:100%" ignore="true" οnkeyup="findInSelect(this.array,this.target);">'+
    '</div>/n'+
    '<div style="'+
     ';position:absolute' +
     ';z-index:1'   +
     ';width:100%'   +
    '">/n' +
     '<iframe  width="100%" height="21" frameborder="1" scrolling="no"></iframe>/n'+
    '</div>/n'+
   '</div>'
 
 document.write(oHtml)
}
drawInput_findInSelect()


//--储存原select中的选项,将其变成数组
var array_findInSelect = new Object()

//--将原来select重构成可查询组件
function create_findInSelect( which ){
 obj  = which.options    //--取得select中的选择项 
 name  = which.name
 
 //--储存原select中的选项
  array_findInSelect['array_'+name] = new Array()
  oArray  = array_findInSelect['array_'+name]
  array  = 'array_findInSelect.array_'+name
  
  for( var i = 0 ; i < obj.length ; i ++ ){
   oArray.push({name:obj[i].text,value:obj[i].value})
  }
 //--end--
 
 oHtml = ''+
   '<input type="button" ignore="true" value="搜索" οnclick="findInSelect('+array+','+name+');">'

 which.outerHTML = which.outerHTML+oHtml
}

 
function findInSelect( array,target){ //--which:输入搜索框,array:下拉选择数组,target:选择后的要放置数值input
 where = draw_findInSelect
 which  = SS_search
 
 txt  = which.value
 opt  = new Array()
 obj  = array
 
 if( txt != "" ){ //--不为空时搜索
  for( var i = 0 ; i < obj.length ; i ++ ){
   if( obj[i].name.indexOf(txt) >= 0 ){
    opt.push('<div value="'+obj[i].value+'" οnmοuseοver="selectFX(this)">'+obj[i].name+'</div>')
   }
  } 
  
 }else{    //--为空时显示全部
  for( var i = 0 ; i < obj.length ; i ++ ){
    opt.push('<div value="'+obj[i].value+'" οnmοuseοver="selectFX(this)">'+obj[i].name+'</div>')
  }
 }
 
 //---取下拉框值
  offset = parseInt(target.currentStyle.borderWidth)
  iTop  = target.getBoundingClientRect().top + target.offsetHeight - offset
  iLeft = target.getBoundingClientRect().left - offset
  iWidth = target.offsetWidth
  iHeight = target.offsetHeight
 //--end---
 
 
 //--关于搜索输入框--
  SS_search_Div.style.display = ''
  SS_search_Div.style.top  = iTop - target.offsetHeight - 1
  SS_search_Div.style.left  = iLeft
  SS_search_Div.style.width  = iWidth
  SS_search_Div.style.height  = iHeight
  SS_search.array  = array
  SS_search.target  = target
 //--end--
 
 
 if( target.form != null ){
  oTarget = target.form.name+"."+target.name
 }else{
  oTarget = target.uniqueID
 }
 
 sHtml = '<div id="SS_select"  onselectstart="return false"'+
   ' style="'+
    ';font:9pt'    +
    ';position:absolute' +
    ';z-index:100'   +
    ';top:'  + iTop  +
    ';left:' + iLeft  +
    ';width:' + iWidth +
    ';cursor:default'  +
   '" οnclick="'+
     ';'+oTarget+'.value=event.srcElement.value' +   //--点击取得值
     ';SS_select.style.display = /'none/''  +   //--自定义下拉框隐藏
     ';SS_search_Div.style.display= /'none/'' +   //--搜索框隐藏
   '">/n' +
    '<div id="SS_option" style="' +
     ';border:1px solid black' +
     ';position:absolute'  +
     ';z-index:2'    +
     ';width:100%'    +
     ';overflow:auto;'   +
    '">/n' +
     '<div value="" style="color:red">共['+opt.length+']项</div>/n' +
     opt.join('') +
    '</div>/n'+
    '<div id="SS_frame" style="'+
     ';position:absolute' +
     ';z-index:1'   +
     ';width:100%'   +
     ';overflow:hidden;'  +
    '">/n' +
     '<iframe  width="100%" height="100%" frameborder="0" scrolling="no"></iframe>/n'+
    '</div>/n'+
   '</div>/n'
   
 where.innerHTML = sHtml 
 
 //--设定下拉框高度--
  if( SS_option.offsetHeight > document.body.offsetHeight ){
   SS_option.style.height = document.body.offsetHeight/2
  }else{
   SS_option.style.height = "auto"
  }
 //--设定下拉框后的iframe高度
   SS_frame.style.height = SS_option.offsetHeight
 //--end--
 
}

//--隐藏自定义下拉框
function findInSelect_hide(){
 if( event && document.all("SS_option")!=null ){
  if( event.srcElement == SS_option || event.srcElement.parentNode == SS_option || event.srcElement.ignore == "true" ){
   return
  }else{
   SS_select.style.display = 'none'
   SS_search_Div.style.display = 'none'
  }
 }
}


//--选择效果
var lastOver_findInSelect = null
function selectFX( which ){
 obj = lastOver_findInSelect
 if( obj != null && which != obj ){
  obj.style.cssText="color:windowtext;background:white;"
 }
 which.style.cssText="color:highlighttext;background:highlight;"
 lastOver_findInSelect = which
}

******html部份******
<script language="jscript" src="findInSelect.js"></script>
<script >
function selectOnload(){
    create_findInSelect(form1.aaa)
}
</script>

<body onLoad="selectOnload()" οnmοusedοwn="findInSelect_hide()">
    <form name="form1">
        <select name="aaa">
            <option value="Windows/.NET">Windows/.NET</option>
            <option value="C/C++">C/C++</option>
            <option value="Java">Java</option>
            <option value="Web开发(脚本和动态语言)">Web开发(脚本和动态语言)</option>
            <option value="Linux/Unix/BSD">Linux/Unix/BSD</option>
            <option value="数据库开发应用">数据库开发应用</option>
            <option value="游戏开发">游戏开发</option>
            <option value="移动开发">移动开发</option>
            <option value="汇编">汇编</option>
            <option value="软工与管理">软工与管理</option>
        </select>
    <form>

</body>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
A:要实现搜索下拉框,可以使用 iview 组件库中的 Select 组件和 Input 组件结合使用,具体实现如下: 1. 引入 Select 和 Input 组件: ``` <template> <div> <Select v-model="selectedValue" :filterable="true"> <Option v-for="(item, index) in options" :value="item.value" :key="index">{{item.label}}</Option> </Select> </div> </template> <script> import {Select, Option} from 'view-design' export default { components: { Select, Option }, data() { return { selectedValue: '', // 默认选中项 options: [{value: 'option1', label: '选项1'}, {value: 'option2', label: '选项2'}, {value: 'option3', label: '选项3'}] } } } </script> ``` 2. 在 Select 组件中加入 :filterable="true" 属性,开启可搜索模式。 3. 在 Select 组件中使用 v-model 指令绑定选中项的值,即双向绑定数据。 4. 在 Select 组件中使用 v-for 指令遍历选项列表,并使用 Option 组件渲染每个选项,同时设置 :value 属性和 :key 属性,分别绑定选项的值和唯一标识符。 5. 如果需要自定义下拉框搜索框的样式,可以使用 Select 组件的 scoped-slot ,例如: ``` <template> <div> <Select v-model="selectedValue" :filterable="true"> <template slot="input"> <Input suffix-icon="arrow-dropdown" placeholder="请输入搜索内容" /> </template> <Option v-for="(item, index) in options" :value="item.value" :key="index">{{item.label}}</Option> </Select> </div> </template> <script> import {Select, Option, Input} from 'view-design' export default { components: { Select, Option, Input }, data() { return { selectedValue: '', options: [{value: 'option1', label: '选项1'}, {value: 'option2', label: '选项2'}, {value: 'option3', label: '选项3'}] } } } </script> ``` 6. 在 Select 组件中使用 slot="input" 属性,自定义搜索框的样式及事件。 7. 在自定义搜索框的 Input 组件中使用 suffix-icon 属性添加下拉箭头图标,使用 placeholder 属性添加搜索框提示文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

u2g2000

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值