简体   繁体   English

如何检查用户是否输入了打字值

[英]How to check if user has entered typeahed value

I have the following code: 我有以下代码:

 var students = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('fullName'),
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                remote: {
                    url: '/api/students?query=%QUERY',
                    wildcard: '%QUERY'                                                                                                               
               } 

            });


            $('#student').typeahead({
                minLength: 2,  
                highlight: true,  

            },
                {
                    name: 'students',
                    display: function (item) { return item.fullName },
                    source: students.ttAdapter(),

                    templates: {
                        empty: [

                            '<div class="tt-empty-message">' +
                            'No results found' +
                            '</div>'
                        ]                       
                    }

                }).on("typeahead:select",
                function (e, student) {

                    console.log("inside isselect");
                    isSelected = true;
                    vm.studentId = student.id;

                });

I am trying to capture a scenario where if a user enters some gibberish or something apart from typeahead values and clicks on submit button, then I need to display an error message. 我正在尝试捕获一个场景,如果用户输入了一些乱码或除预输入值之外的其他内容,然后单击“提交”按钮,那么我需要显示一条错误消息。 To do that, I need to capture the typeahead list and see if the typeeahed list contains the input value. 为此,我需要捕获提前输入列表,并查看已提前输入的列表是否包含输入值。 How can I accomplish this? 我该怎么做?

I have searched for this solution for 5 hours now. 我已经搜索了此解决方案5个小时了。 I have looked and implemented similar answers on other threads but nothing seemed to work. 我在其他线程上查找并实现了类似的答案,但似乎没有任何效果。 For instance take a look at this answer: 例如看一下这个答案:

http://jsfiddle.net/Fresh/bbzt9hcr/ http://jsfiddle.net/Fresh/bbzt9hcr/

I tried the baove code but the typeahed does not even come up. 我尝试了baove代码,但打字错误甚至没有出现。 Whereas my code works perfectly fine. 而我的代码运行得很好。 Please let me know how I can solve this. 请让我知道我该如何解决。 Thank you in advance. 先感谢您。

There are multiple way to approach this, the simplest: you could listen the typeahead change event and use a variable to store the selected value, then on submit check and send that variable: http://jsfiddle.net/alfredopacino/bbzt9hcr/290/ . 有多种方法可以解决这个问题,最简单的方法是:您可以侦听typeahead更改事件并使用变量存储所选值,然后在提交检查并发送该变量时: http : //jsfiddle.net/alfredopacino/bbzt9hcr/290 /

yourTypehead.on('typeahead:selected', function($e, datum) {  // suggestion selected
      selectedMovie = datum.value
});
$('#submit').click(function() {
   if(selectedMovie) {
                //your ajax call
   } else {
       alert("no valid movie")
   }

});

The problem with this is it doesn't covers the case the user enters manually a valid value. 这样做的问题是它不能涵盖用户手动输入有效值的情况。 To cover this scenario you could follow two ways: 要解决此情况,您可以采用以下两种方法:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM