具有ajax的select2清除之前对焦点的响应

时间:2016-04-05 10:24:27

标签: jquery ajax select2

使用select2

$("#select").select2({
  ajax: {
    url: "/getcity",
    dataType: 'json',
    type: 'post',
    delay: 250,
    allowClear: false,
    minimumInputLength: 3,
    cache: true
  }
});

我每次关注时都会提出没有参数的请求,只是网址。因此,如果在先前的良好请求中我已经收到了良好的数据,则它会在选择中显示,在下一个焦点上选择2,在select中发出空请求和清除数据。如何防止它?

1 个答案:

答案 0 :(得分:1)

您应该在minimumInputLength之外移动ajax和其他一些选项,因为它们不是ajax选项。在你的情况下,select2永远不会得到选项minimumInputLength,并且每次获得焦点时都会调用ajax。

$("#select").select2({
  ajax: {
    url: "/getcity",
    dataType: 'json',
    type: 'post',
    delay: 250,
    cache: true
  },
  allowClear: false,
  minimumInputLength: 3
});