开发者

How to check in an entered value is from autocomplete?

I have a jquery's autocomplete texbox and, upon a click of a button, I need to check if the value entered has come from the autocomplete or is it a completely new value.

The problems is that my code constantly gives me 'alert("no")'... maybe I shouldn't be checking against 'cache', but something else?

//////////////////////////////////////////// autocomplete code //////////////////
        var cache = {},
            lastXhr;
        $( "#inputV" ).autocomplete({
            minLength: 2,
            source: function( request, response ) {
                var term = request.term;
                if ( term in cache ) {
                    response( cache[ term ] );
                    return;
                }

                lastXhr = $.getJSON( "search.php", request, function( data, status, xhr ) {
                    cache[ term ] = data;
                    if ( xhr === lastXhr ) {
                        response( data );
                    }
                });
            }
        });


////////////////////////// check if input comes from autocomplete  //////////////////
        $('#btn_check').click(function()        {

            var input = $("#inputV").val();
            alert(input);

            if ( input in cache ) 
                {
                    alert("yes");
                 }  
              开发者_如何学Python  else
                {
                    alert("no");
                }   
        });

How to check in an entered value is from autocomplete?


This is how I make it on a project of mine

function lookup(inputString) {
    $.post('/getSuggestions', {
        queryString: ""+inputString+""
    }, function(data){
        if(data.length >0) {
            $("#suggestions").show();
            $("#autoSuggestionsList").html(data);
        }
    });
}

function fill(thisValue,thisID) {
    $("#inputString").val(thisValue);
    $("#id").val(thisID);
    $("#suggestions").hide();
}

Where the fill() function is attached to returned results from the autocomplate. So if user clicks on an item from the list, the fill function will set the value in my html element with id "id" using this $("#id").val(thisID);

Simply if user doesn't use the suggest from the list, the "id" will not be filled. In your case you can just add an hidden input with name/id "isacutocomplate", attach fill(); function to results returned from the autocomlate and if item is clicked, you will update the "isacutocomplate", other way you will not fill it. So, when user click your button, you check the "isacutocomplate" input value and you see if it's from autocomplate's list or not.

Hope that make sense to you. If not, I can provide entire process.


//var cache = {} // doesnt make sense.. change it to:
var cache = []

 ...
 //and change this to:
 if ( $.inArray(input,cache) != -1 ) 
            {
                alert("yes");
             }  
            else
            {
                alert("no");
            }  


You can try a little manipulation. When a text box in on focus, you set a counter "keypress". On every key press (except UP and DOWN arrow buttons), you increment the counter. Meanwhile jquery autocomplete will show some suggestions and suppose if the user selects a value, then you check the length of the text of the textbox on the onchange event.

If "keypress" counter value is less than length of the text, then it was from autocomplete.

Hope this fits in your case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜