最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - autocomplete check when focus is lost - Stack Overflow

programmeradmin0浏览0评论

How do I ensure the value in the input field always has a value from the source only?

For example, if in the source I have

source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]

which is database driven as the actual script is:

source: "get_json.aspx";

and in the input field I type just "cold", how do I stop this from happening?

How do I ensure that the value exists in the source when focus is lost from the input field?

How do I ensure the value in the input field always has a value from the source only?

For example, if in the source I have

source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]

which is database driven as the actual script is:

source: "get_json.aspx";

and in the input field I type just "cold", how do I stop this from happening?

How do I ensure that the value exists in the source when focus is lost from the input field?

Share Improve this question edited Apr 17, 2014 at 16:34 Lee 31.1k31 gold badges122 silver badges184 bronze badges asked Jan 28, 2011 at 10:45 oshirowanenoshirowanen 16k83 gold badges205 silver badges356 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You should bind to proper event. I think you should bind to close event. then check value of input contains element in array. Else you should make input empty again using val. I tested it myself locally and it does the trick.

$( "#tags" ).autoplete({
    source: availableTags,
    close: function(event, ui) {
        var inputValue = $( "#tags" ).val();
        var idx = jQuery.inArray(inputValue, availableTags);
        if (idx == -1) {
            $( "#tags" ).val("");           
        }
    }
});

P.S: I did some more testing and I think you should bind it to another event instead. Because when you tab to navigation bar it does not work. I think you should bind to blur event instead.

The improved code =>

$( "#tags" ).autoplete({
    source: availableTags
});

$( "#tags").blur(function() {
    var inputValue = $( "#tags" ).val();
    var idx = jQuery.inArray(inputValue, availableTags);
    if (idx == -1) {
        $( "#tags" ).val("");           
    }
});

json to array =>

json.txt:

[{"id":1,"value":"ActionScript"},{"id":2,"value":"AppleScript"},{"id":3,"value":"Asp"},{"id":4,"value":"BASIC"},{"id":5,"value":"C"},{"id":6,"value":"C++"},{"id":7,"value":"Clojure"},{"id":8,"value":"COBOL"},{"id":9,"value":"ColdFusion"},{"id":10,"value":"Erlang"},{"id":11,"value":"Fortran"},{"id":12,"value":"Groovy"},{"id":13,"value":"Haskell"},{"id":14,"value":"Java"},{"id":15,"value":"JavaScript"},{"id":16,"value":"Lisp"},{"id":17,"value":"Perl"},{"id":18,"value":"PHP"},{"id":19,"value":"Python"},{"id":20,"value":"Ruby"},{"id":21,"value":"Scala"},{"id":21,"value":"Scheme"}]

$(function() {
    var LIMIT = 10;
    $.getJSON("json.json", function(data) {
        var autoplete = $( "#tags" ).autoplete({
            source: function( request, response ) {
                var i=0;
                var result = [];
                $.each(data, function(index, value) {
                    // value.value.search(/request.term/i);
                    var idx = value.value.toLowerCase().indexOf( request.term.toLowerCase() );
                    if (idx >= 0) {
                        result.push(value.value);
                        i++;
                    }
                    if (i === LIMIT) {
                        return false;
                    }
                });
                response(result);
            }
        });

        $( "#tags").blur(function() {
            var inputValue = $( "#tags" ).val();
            var clear = true;
            $.each(data, function(index, value) { 
                if (value.value == inputValue) {
                    clear = false;
                    return false;
                }
            });
            if (clear) {
                $( "#tags" ).val("");
            }
        });
    });
});
$("#tags").autoplete({
      source: availableTags,
      autoFocus: true,
      selectFirst: true,
      //clear value on lost focus
      change: function (ev, ui) {
        if (!ui.item) {
            $(this).val('');
        }
      }
    })
发布评论

评论列表(0)

  1. 暂无评论