问题
I have this json file on /api/searches
:
[{"id":"513dbb61a61654a845000005","text":"ingeniero agrónomo"},{"id":"513a11d4a6165411b2000008","text":"ingeniero industrial"}]
I'm using select2 to show results.
This is my current select2 setting:
$('#query_txt').select2
createSearchChoice: (term, data) ->
if $(data).filter(->
@text.localeCompare(term) is 0
).length is 0
id: term
text: term
width: 'resolve'
minimumInputLength: 3
tags: true
showSearchBox: true
maximumSelectionSize: 1
closeOnSelect: true
multiple: false
selectOnBlur: true
ajax:
url: "/api/v1/cvs/searches"
quietMillis: 100
dataType: 'json'
data: (term, page) ->
q: term,
page_limit: 9
results: (data, page) ->
results: data
initSelection: (element, callback) ->
$.get "/api/searches/" + element.val(), (data) ->
callback {id: data.id, text: data.occupation}
In this example, http://jsfiddle.net/nqWNJ/6/ or http://jsfiddle.net/uDrvp/1/ if you type on text field the word "asdfasdf"
or random word, and if you click output of the input field, the keyword remains inside the input and this keyword is not deleted by select2.
I would like, if no results are found or the user clicks outside the text field, the keyword is not removed and my form is not disabled
How can I achieve this purpose?
回答1:
you have to add selectOnBlur:true
option to your config and upgrade to latest master. here is a working fiddle: http://jsfiddle.net/nqWNJ/7/
来源:https://stackoverflow.com/questions/15436782/select2-deny-to-can-clean-the-input-field-and-disable-the-form-if-results-not