jQuery 设置预先输入的选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17278709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set selected value of typeahead
提问by gpasse
I am using the twitter bootstrap typeahead.
我正在使用twitter bootstrap typeahead。
I have customized the typehead so I can select a pair value/label
我已经自定义了打字头,所以我可以选择一对值/标签
$.each(data.medicines,function(index,values){
_med=new Object()
_med.value=values.LYF
_med.id=values.LYF_NUMER
_medicines.push(_med)
})
//Fill typeahead with data
$('.typeahead').typeahead({
// note that "value" is the default setting for the property option
source:_medicines,
onselect: function(obj) { showdetails(obj) }
})
How can I set the selected value using Javascript ?
如何使用 Javascript 设置选定的值?
For exemple $("#mytypehead").val(43);
回答by Matt Ball
According to the typeahead.js
documentation on GitHub(which is much more detailed than the bootstrap-typeahead.js
docs in Twitter Bootstrap):
根据GitHub 上的typeahead.js
文档(比Twitter Bootstrap 中的bootstrap-typeahead.js
文档详细得多):
jQuery#typeahead('setQuery', query)
Sets the current query of the typeahead. This is always preferable to using
$("input.typeahead").val(query)
, which will result in unexpected behavior. To clear the query, simply set it to an empty string.
jQuery#typeahead('setQuery', 查询)
设置预先输入的当前查询。这总是比使用 更可取
$("input.typeahead").val(query)
,后者会导致意外行为。要清除查询,只需将其设置为空字符串。
2016 Update (setQuery does not exist anymore) :
2016 更新(setQuery 不再存在):
jQuery#typeahead('val', val) Sets the value of the typeahead. This should be used in place of jQuery#val.
jQuery#typeahead('val', val) 设置预先输入的值。这应该用来代替 jQuery#val。
$('.typeahead').typeahead('val', myVal);