jquery自定义实现select搜索下拉功能

232 篇文章 0 订阅
68 篇文章 0 订阅

容易看懂 的就直接贴代码了。

<form action="">
<div class="o-mis0519-alert">
    <table class="o-mis0519-table-a" border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td align="left" width="100">设备编号</td>
            <td>
<!--                <div class="searchshowres" style="display:none;">-->
<!--                    <div class="searchres">1111</div>-->
<!--                </div>-->
                <input class="searchtxt" style="width:200px;">
                <select name="device_code" id="device_code" class="searchsel">
                    <option value="">-------请选择-------</option>
                 <?php if($device_codes):foreach ($device_codes as $val):?>
                    <option value="<?=$val;?>"><?=$val;?></option>
                 <?php endforeach;endif;?>
                </select>
            </td>
        </tr>
    </table>
</div>
    <input type="hidden" name="post_id" id="post_id" value="<?=$post_id;?>">
    <p class="o-mis-p2-submite"><input type="submit" id="sub" value="提  交" class="" /></p>
</form>
<script>
    $('#sub').click(function () {
        var post_id = $('#post_id').val();
        var device_code = $('#device_code').val();

        if (!post_id) {
            $.dialog.tips('缺少参数',2,'error.gif');
        }

        $.ajax({
            url : "<?=Yii::$app->urlManager->createUrl('/Zhibo/workroom/dobinddevice'); ?>",
            type : 'post',
            dataType : 'json',
            data : {'post_id' : post_id, 'device_code' : device_code},
            success : function (res) {
                if (1 == res.status) {
                    $.dialog.tips(res.info,2,'success.gif');
                    window.location.reload();
                } else {
                    $.dialog.tips(res.info,2,'error.gif');
                }
            }
        });
        return false;
    })

</script>

<script>
    $(function(){
        var alloption = $('#device_code').html();

        var alloptionarr = [];

        $(".searchsel").children("option").each(function(){
            alloptionarr.push(this);
        });

        $(".searchtxt").on("keyup", function(){
            var $stxt = $(this);
            var value = $stxt.val();
            var otxt = '';

            //遍历select的option的文本中包含输入的文本内容的option集合
            // $stxt.next(".searchsel").children("option:contains('"+value+"')").each(function(){
            //     console.log(23);
            //     // otxt += "<span class='searchresval' code='"+$(this).attr("value")+"' style='width:147;cursor:hand' >"+$(this).text()+"</span><br>";
            //     otxt += "<option value='"+ $(this).attr("value") +"'>" + $(this).attr("value") + "</option>";
            // });
            $(alloptionarr).each(function(data,val){
                var attrvaulue = $(val).attr('value');
                if (attrvaulue.indexOf(value) != -1 && value) { //如果option的文本中包含输入的文本内容
                    otxt += "<option value='"+ attrvaulue +"'>" + attrvaulue + "</option>";
                }
            });

            //如果需要检索的值为空
            if (!value) {
                // console.log(alloption);
                $('#device_code').empty().html(alloption)
            } else {
                $stxt.next(".searchsel").empty().append(otxt);
            }
            //在select框下展现搜索的结果
            // $stxt.prev(".searchshowres").show()
            //     .children(".searchres").empty().append(otxt)
            //     .children('.searchresval').bind("mouseover", function(){//鼠标移到移出索索结果上的一个颜色变化
            //     $(this).css("background", "#cccccc");
            // }).bind("mouseout", function(){
            //     $(this).css("background", "#ffffff");
            // }).bind("click", function(){
            //     $stxt.val($(this).text());
            //     $stxt.prev(".searchshowres").hide();
            //     $stxt.next(".searchsel").children("option[value='"+$(this).attr("code")+"']").attr("selected", true);
            //
            // });


        });

        // $(".searchSel").on("change", function(){
        //     $(this).prev(".searchtxt").val($(this).find("option:selected").text());
        // });
    });
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值