开发者

text field lock in jquery auto-complete - only allow submissions within autocomplete options

Hei, am looking for help in my code that allows an autocomplete, only within a specified data array. So only data within the auto complete array is allowed to be entered. for example if user has typed iran01 then user wont allow to type 01 because iran01 is not valid . or for example if user type NNN after iran then it wud be iranNNN which is also not valid . Then only iran shud be in textbox which is valid entry . can anyone tell how to do this.

can any one tell how to get this behavior .

$(document).ready(function () {
                     data = ["iran", "iraq", "Libya", "india"];
                    $("#autocomplet开发者_开发问答e").autocomplete(data)
               });

               $('input#autocomplete').bind('keypress keyup', function(){

                 if ($.inArray($(this).val(), data) == -1){
                    $(this).val('');
                    $("#result").html("No match found!" + $(this).val());
              }

           });

            $('input#autocomplete').result(function (event, data, formatted) {
                $("#result").html(!data ? "No match found!" : "Selected: " + formatted);

            }).keyup(function () {
                $(this).search();

            });

         } 
    });


You can put put a listener on the change event of your input box and then make sure an element has been selected. Check out my answer to this question:

jquery autocomplete field that REQUIRES a selected value?

http://jsfiddle.net/vZeHr/4/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜