Select2 - Deny to can clean the input field and disable the form if results not found

天涯浪子 提交于 2019-12-12 02:46:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!