选择2 - 拒绝可以清除输入字段并禁用表单,如果找不到结果 [英] Select2 - Deny to can clean the input field and disable the form if results not found

查看:302
本文介绍了选择2 - 拒绝可以清除输入字段并禁用表单,如果找不到结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 / api / searches 上有这个json文件:

  [{id:513dbb61a61654a845000005,text:ingenieroagrónomo},{id:513a11d4a6165411b2000008,text:ingeniero industrial}] 



这是我目前的select2设置:

  $('#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}

http://jsfiddle.net/nqWNJ/6/ http://jsfiddle.net/uDrvp/1/ 如果您在文本字段上输入单词asdfasdf或随机字,如果您点击输入字段的输出,关键字仍保留在输入内,且该关键字不会被select2 删除



我想,如果没有找到结果或用户在文本字段外点击,则关键字不会被删除,我的表单也不会被停用



我如何实现这个目的?

解决方案

您必须添加 selectOnBlur:true 升级到最新主。这里是一个工作的小提琴: http://jsfiddle.net/nqWNJ/7/


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?

解决方案

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/

这篇关于选择2 - 拒绝可以清除输入字段并禁用表单,如果找不到结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆