Bootstrap令牌字段和Twitter未来样式下拉列表未设置样式

时间:2017-05-24 13:55:08

标签: javascript css twitter-bootstrap typeahead.js bootstrap-tokenfield

我有一个与this one类似的问题,但我同时使用Tokenfield v0.12.1Twitter typeahead v0.11.1。当我开始输入并显示建议时,它们没有样式。它显示为纯文本(如果我点击建议,它会添加到输入字段)。例如,如果我输入“re”,则显示“red”的建议,但它显示为无格式,如下所示:

<input id="num" type="number">

我的CSS源文件

{"_query":"re","value":"red","label":"red"}

我的JS源文件

<link href="~/Content/bootstrap-tokenfield/tokenfield-typeahead.css" rel="stylesheet" />
<link href="~/Content/bootstrap-tokenfield/bootstrap-tokenfield.css" rel="stylesheet" />

我的HTML

<script src="~/Scripts/bootstrap-tokenfield.min.js"></script>
<script src="~/Scripts/typeahead.bundle.min.js"></script>
<script>
$(document).ready(function () {
    var engine = new Bloodhound({
        local: [{ value: 'red' }, { value: 'blue' }, { value: 'green' }, { value: 'yellow' }, { value: 'violet' }, { value: 'brown' }, { value: 'purple' }, { value: 'black' }, { value: 'white' }],
        datumTokenizer: function (d) {
            return Bloodhound.tokenizers.whitespace(d.value);
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace
    });

    engine.initialize();

    $('#tokenfield-typeahead').tokenfield({
        typeahead: [null, {
            source: engine.ttAdapter()
        }]
    });
});
</script>

我的摘要如下所示。请帮助我做错了什么?

<input type="text" class="typeahead form-control" id="tokenfield-typeahead" />
$(document).ready(function() {
  var engine = new Bloodhound({
    local: [{
      value: 'red'
    }, {
      value: 'blue'
    }, {
      value: 'green'
    }, {
      value: 'yellow'
    }, {
      value: 'violet'
    }, {
      value: 'brown'
    }, {
      value: 'purple'
    }, {
      value: 'black'
    }, {
      value: 'white'
    }],
    datumTokenizer: function(d) {
      return Bloodhound.tokenizers.whitespace(d.value);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace
  });

  engine.initialize();

  $('#tokenfield-typeahead').tokenfield({
    typeahead: [null, {
      source: engine.ttAdapter()
    }]
  });
});

1 个答案:

答案 0 :(得分:2)

  

例如,如果我输入&#34; re&#34;,建议&#34; red&#34;出现,但它显示如下:

     

{&#34; _query&#34;:&#34;再&#34;&#34;值&#34;:&#34;红色&#34; ...

为了避免您需要将 displayKey 选项添加到令牌字段

$('#tokenfield-typeahead').tokenfield({
    typeahead: [null, {
        source: engine.ttAdapter(),
        displayKey: 'value'
    }]
});

&#13;
&#13;
$(document).ready(function() {
    var engine = new Bloodhound({
        local: [{
            value: 'red'
        }, {
            value: 'blue'
        }, {
            value: 'green'
        }, {
            value: 'yellow'
        }, {
            value: 'violet'
        }, {
            value: 'brown'
        }, {
            value: 'purple'
        }, {
            value: 'black'
        }, {
            value: 'white'
        }],
        datumTokenizer: function(d) {
            return Bloodhound.tokenizers.whitespace(d.value);
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace
    });

    engine.initialize();

    $('#tokenfield-typeahead').tokenfield({
        typeahead: [null, {
            source: engine.ttAdapter(),
            displayKey: 'value'
        }]
    });
});
&#13;
.tt-query, 
.tt-hint {
    width: 396px;
    height: 30px;
    padding: 8px 12px;
    font-size: 24px;
    line-height: 30px;
    border: 2px solid #ccc;
    border-radius: 8px;
    outline: none;
}

.tt-query { 
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
}

.tt-hint {
    display: none;
}

.tt-menu { 
    width: 422px;
    margin-top: 12px;
    padding: 8px 0;
    background-color: #fff;
    border: 1px solid #ccc;
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    box-shadow: 0 5px 10px rgba(0,0,0,.2);
}

.tt-suggestion {
    padding: 3px 20px;
    font-size: 18px;
    line-height: 24px;
}

.tt-suggestion.tt-is-under-cursor { 
    color: #fff;
    background-color: #0097cf;

}

.tt-suggestion p {
    margin: 0;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tokenfield/0.12.0/css/bootstrap-tokenfield.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tokenfield/0.12.0/css/tokenfield-typeahead.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tokenfield/0.12.0/bootstrap-tokenfield.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>



<div class="row">
    <div class="col-lg-12">
        <dl class="dl-horizontal">
            <dt>Action Items:</dt>
            <dd>
                <div id="action-items">
                    <input type="checkbox" name="item1" value="item1"> Action Item 1<br>
                    <form class="form-inline">
                        <div class="tokenfield form-group">
                            <input type="text" class="typeahead form-control" id="tokenfield-typeahead" />
                        </div>
                        <button type="button" class="btn btn-default">Save</button>
                    </form>
                    <input type="checkbox" name="item2" value="item2"> Action Item 2<br>
                    <input type="checkbox" name="item3" value="item3"> Action Item 3
                </div>
            </dd>
            <hr />
        </dl>
    </div>
</div>
&#13;
&#13;
&#13;