【发布时间】:2016-11-17 02:42:16
【问题描述】:
虽然可以使用 createSearchChoice 将新值添加到 Select2 (3.5) 元素中,但如果新值不在列表中,我如何才能获得要显示的新值?我可以添加不在 Select2 列表中的值,但是如何显示不在列表中的默认值?
$("#select").select2({
placeholder: "Who is the invoice from?",
minimumInputLength: 2,
quietMillis: 300,
allowClear : true,
ajax: {
url: "accountsDataStore.xsp",
dataType: 'json',
data: function (term, page) {
return {
q: term, // search term
page_limit: 10
};
},
results: function (data, page) { return data; }
},
// extra code to allow entering value not in list
id: function(object) { return object.text; },
//Allow manually entered text in drop down.
createSearchChoice:function(term, data) {
if ( $(data).filter( function() {
return this.text.localeCompare(term)===0;
}).length===0) {
return {id:term, text:term};
}
},
initSelection: function(element, callback) {
//sets a default value (if a value was selected previously)
//get the existing value
var id = $(element).val();
//if a value was selected: perform an Ajax call to retrieve the text label
if (id !== "") {
$.ajax(
"accountsDataStore.xsp", {
data: { id: id },
dataType: "json"
}).done(function(data) {
// ** TODO if value not found, display current element value -- HOW?? **
callback(data); });
}
}
}).on('change', function (evt) {
// Do something after value changes
}
);
initSelection期间,如果没有找到默认值,那么如何显示当前值?
<input type="hidden"
id="view:_id1:_id4:callback1:inputName"
name="view:_id1:_id4:callback1:inputName"
aria-required="true"
value="ACME Company"
tabindex="-1"
class="select2-offscreen">
在表单中,设置了值,它只是不显示在出现选定值的<span> 中。我们只是以某种方式将其添加到列表中吗?
谢谢。
【问题讨论】:
标签: twitter-bootstrap xpages select2