【问题标题】:how can typehead allows only list values else give alert?打字头如何只允许列出值,否则会发出警报?
【发布时间】:2019-01-27 05:04:31
【问题描述】:

我正在使用 typeahead.js 0.9.3,是否可能 typehead 只允许列表中的项目而不是用户手动输入的文本。它不允许用户这样做或在任何事件之后检查该值是否存在于列表中。

$(document).ready(function(){
  $('input.typeahead').typeahead({
    name: 'typeahead',
    remote:'sale/files/search_part.php?key=%QUERY',
    limit : 10
  });
});

我不擅长 javascript 和 JQuery,所以不知道如何将数组值设置为列表,所以可以在下面的代码中检查它是否存在于列表中。请帮忙

var myData = [...]; // how to assign output list to this array?

$("#partno").typeahead({source : myData}).blur(validateSelection);

function validateSelection() {
  if(source.indexOf($(this).val()) === -1) {
    alert('Error : element not in list!');
  }
}

输入框

<input type="text" name="typeahead" class="typeahead tt-query" autocomplete="off" spellcheck="false"  id="partno" >

【问题讨论】:

  • 您使用的是哪个 typeahead 插件?有几个可用的
  • typeahead.js 0.9.3
  • 您可以使用notFound / empty 自定义模板来显示您的消息。 github.com/twitter/typeahead.js/blob/master/doc/…
  • 感谢您的宝贵时间,但在我的情况下,它们都不起作用,因为“notfound”用于请求查询,“empty”用于查询目的。

标签: javascript jquery typehead


【解决方案1】:

将输出列表分配给 myData

$(document).ready(function() {
var myData = [];
$.ajax({
    url : url_example.php,
    data:{
        condition : 'text'
    },
    dataType:'json',
    method:'POST',
    success:function(result){
        myData = result.data
    },error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log('error!');
    }
})

});

你可以在这里阅读更多 http://api.jquery.com/jquery.ajax/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2022-01-17
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2015-01-06
    相关资源
    最近更新 更多