Javascript 检查文本框中的值是否仅从自动完成值输入。

Javascript 检查文本框中的值是否仅从自动完成值输入。,javascript,jquery,json,autocomplete,dojo,Javascript,Jquery,Json,Autocomplete,Dojo,我正在开发一个web应用程序。对于输入值,我使用一个自动完成,它从数据库中获取数据 $(document).ready(function hello(){ var myVar1 = <%=request.getAttribute("variable1")%> $("input#assignedbyid").autocomplete({ source: myVar1 }); }); 它是自动完成值的来源。 我的html代码,其中我的input标

我正在开发一个web应用程序。对于输入值,我使用一个自动完成,它从数据库中获取数据

$(document).ready(function hello(){
    var myVar1 = <%=request.getAttribute("variable1")%>
      $("input#assignedbyid").autocomplete({
    source: myVar1
      });
});
它是自动完成值的来源。 我的html代码,其中我的
input标记onfocus
调用
函数hello()。

我的其他部分暂时还不完整。这是怎么做到的?。谢谢

var V = $("#myInput").val();
if (V=="Kapil"||V=="Mayur"||V=="Abhinav"||V=="Chandan") {
    //do something
}


如果您使用的是jQuery,则可以使用以下方法:

如果您只是使用普通Javascript,它也很简单:

if(myArray.indexOf(enteredValue) == -1) { ... } // If enteredValue is not in myArray

将此示例代码用于自动完成

示例代码

    var url1 = sitepath+'opportunity/contact_autocomplete/';
        var related= '';
        $(function() {
            $( "#txtcontactid" ).autocomplete(
            {
                source:url1,
                select: function( event, ui ) 
                {
                    log( 
                         ui.item.id 
                      );
                }
            });
        });

像这样手动检查数组中的每一个值的V是草率的,因为自动完成列表可能非常大,或者发生了变化,等等。如果他决定明天自动完成列表应该发生变化,他就不必更新此代码了。
var V = $("#myInput").val();
if (V=="Kapil"||V=="Mayur"||V=="Abhinav"||V=="Chandan") {
    //do something
}
var V = $("#myInput").val();
var myArray = ["Kapil","Mayur","Abhinav","Chandan"];

if ($.inArray(V, myArray)!=-1) {
    //do something
}
if($.inArray(enteredValue, myArray) == false) { ... } // If enteredValue is not in myArray
if(myArray.indexOf(enteredValue) == -1) { ... } // If enteredValue is not in myArray
    var url1 = sitepath+'opportunity/contact_autocomplete/';
        var related= '';
        $(function() {
            $( "#txtcontactid" ).autocomplete(
            {
                source:url1,
                select: function( event, ui ) 
                {
                    log( 
                         ui.item.id 
                      );
                }
            });
        });