jquery - 想要将图像插入到 jQuery 自动完成标签中,但图像 src 以文本形式输出

标签 jquery jquery-ui jquery-ui-autocomplete

    $(function() {
        function log( message ) {
            $( "<div/>" ).text( message ).prependTo( "#log" );
            $( "#log" ).attr( "scrollTop", 0 );
        }

        $( "#city" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function( data ) {
                        response( $.map( data.geonames, function( item ) {
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.name
                            }
                        }));
                    }
                });
            },
            minLength: 2,
            select: function( event, ui ) {
                log( ui.item ?
                    "Selected: " + ui.item.label :
                    "Nothing selected, input was " + this.value);
            },
            open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
            },
            close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
            }
        });
    });
    </script>



<div class="demo">

<div class="ui-widget">
    <label for="city">Your city: </label>
    <input id="city" />
    Powered by <a href="http://geonames.org">geonames.org</a>
</div>

<div class="ui-widget" style="margin-top:2em; font-family:Arial">
    Result:
    <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div>

</div>

我想将图像插入到 jQuery 自动完成标签中,但图像 src 在标签内作为文本输出

我尝试像这样修改 success 函数:

success: function( data ) {
    response( $.map( data.geonames, function( item ) {
        return {
            label: item.name + "<img src='/images/glif.png'/>" + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
            value: item.name
        }
    }));
}

最佳答案

你见过Extensible Autocomplete吗?插入?这应该允许您在标签中使用标记。

关于jquery - 想要将图像插入到 jQuery 自动完成标签中,但图像 src 以文本形式输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4481263/

相关文章:

JavaScript 类型错误 : Cannot read property 'id' of undefined

javascript - 隐藏聊天室脚本直到单击按钮

javascript - 我需要一些 JS 和 Jquery 支持

javascript - 使用 jQuery 在 Canvas 上绘制时更改光标?

jquery - 无法将样式更改为 jQuery :s autocomplete element

jQuery UI 自动完成将术语划分为单独的字符,而不是单词

jquery - 如何从 AngularJS Controller 正确调用 jQuery

javascript - 单击按钮时删除 div - 问题 : removing all divs

javascript - Angular UI Sortable 无法正常工作

jquery-ui - jquery 自动完成焦点事件项未定义