-1

我有一个文本字段,用户可以在其中搜索地点名称。如何突出显示结果上的文本,不包括结果中显示的用户输入的内容?以下是我在 jQuery 中的代码:

<input maxlength="64" id="war_desc" name="searchtext" size="20" class="form200" autocomplete="off" value="">

<div class="auto-complete autocomplete_choices" data-component-bound="true" style="top: 352px; left: 352.5px; width: 172px; display: none;">
<ul>
        <!-- Search result will be append here by html() -->
</ul>
</div>

<script type="text/javascript">
$('#war_desc').keyup(function(e){
    var search_place = $(this).val();
    if(search_place == ''){
        $('.autocomplete_choices').hide();
    } else {
        $.ajax({
            url:'".$baseUrl."business/searchnew"."',
            data:{'search_place':$(this).val()},
            dataType:'json',
            Type:'POST',
            cache:false,
            success:function(data){

                var listHtml = '';
                for(var i=0; i<data.length; i++){
                listHtml += '<li class="item" data-display-value="Shoe Exchange" title=Shoe Exchange>'+data[i]['business_name']+'</li>';
            }
                $('.autocomplete_choices').show();
                $('.autocomplete_choices ul').html(listHtml);
            }
        });
    }
})
</script>

以下是描述我的问题的图像:

在此处输入图像描述

谢谢!!

4

2 回答 2

0

You can use this simple string replace method

var str = "Hello zlippr";
str = "Hello"+str.replace(str,"<b>"+str+"</b>").replace("Hello","")
document.writeln(str)

Your code will be like this

data[i]['business_name'] = $(this).val()+data[i]['business_name'].replace(data[i]['business_name'],"<b>"+data[i]['business_name']+"</b>").replace($(this).val(),"")
'<li class="item" data-display-value="Shoe Exchange" title=Shoe Exchange>'+data[i]['business_name']+'</li>';
于 2013-03-08T04:14:03.317 回答
0

使用peterm找到的答案here

使用 jQuery 突出显示简单的文本

将最后一行从

$(this).html(text.replace(new RegExp("(" + keywords.join('|') + ")" , 'gi'), "<b>$1</b>"));

$(this).html('<b>' + text.replace(new RegExp("(" + keywords.join('|') + ")" , 'gi'), "</b>$1<b>")) + '</b>';

演示

于 2013-03-08T03:45:21.570 回答