【问题标题】:input texts dependant with each other with autocomplete feature使用自动完成功能输入相互依赖的文本
【发布时间】:2017-04-14 08:09:17
【问题描述】:

我创建了 2 个输入文本,一个是 ID,另一个是名称。如果我在第一个输入文本中键入一个 ID,然后按 Tab 或单击第二个输入文本(在 HTML 中使用 onfocusout),第二个输入文本将自动填充分配给该 ID 的名称。例如,输入 ID '001' 将显示 'Elnora'。同样,在第二个输入文本中键入“Soo”(然后按 Tab)将在第一个输入文本中显示“010”。基本上它是基于索引号的一对一映射。正如您在我的Jsfiddle 中看到的那样,这完美无缺。

    var scholaridlists = ["001","002","003","004","005","006","007","008","009","010"];
    var scholarlists = ["Elnora","Henry","Scotty","Bruna","Shirley","Modesto","Lissa","Davida","Margherita","Soo"];

 function idtoname() {
    var ids=document.getElementById("currentscholaridlist").value;
    var a = scholaridlists.indexOf(ids);
    document.getElementById("currentscholarlist").value =scholarlists[a];
}

 function nametoid() {
    var names=document.getElementById('currentscholarlist').value;
    var b = scholarlists.indexOf(names);
    document.getElementById('currentscholaridlist').value = scholaridlists[b];
}

但是,由于不是每个人都记得任何人的 ID 和/或姓名,我还想实现一个自动完成功能,这样每当有人输入 ID/姓名时,就会出现建议的 ID/姓名列表。我正在尝试像在我的其他 Jsfiddle 中一样使用 JQueryUI 自动完成功能。自动完成功能有效,但按 Tab/单击其他输入文本不会显示其他分配的配对。

 $( function() {
     "use strict";

var scholaridlists = ["001","002","003","004","005","006","007","008","009","010"];
$( "#currentscholaridlist" ).autocomplete({      source: scholaridlists, minLength:3, maxShowItems:5    });


   var scholarlists = ["Elnora","Henry","Scotty","Bruna","Shirley","Modesto","Lissa","Davida","Margherita","Soo"];
  $( "#currentscholarlist" ).autocomplete({      source: scholarlists, minLength:3, maxShowItems:5    });

} ); 

 function idtoname() {
    var ids1=document.getElementById("currentscholaridlist").value;
    var a = scholaridlists.indexOf(ids1);
    var ids2= a;
    document.getElementById("currentscholarlist").value =scholarlists[ids2];
}

 function nametoid() {
    var names1=document.getElementById('currentscholarlist').value;
    var b = scholarlists.indexOf(names1);
    var names2 = b;
    document.getElementById('currentscholaridlist').value = scholaridlists[names2];
} 

如果有人对此问题有解决方案,我希望 Ids/names 数组列表保留在 JS 中,而不是使用 select/option 的 HTML 中。此外,ID 不一定是数字和字母/编号顺序,如 Jsfiddle 所示(我可以使用 A123、SC001A 等 ID)。

提前致谢!

【问题讨论】:

    标签: javascript jquery html input autocomplete


    【解决方案1】:

    这里需要做一些更改。

    使用onblur

    HTML

    <input type="text" id="currentscholaridlist" onblur="idtoname()">
    <br/>
    <input type="text" id="currentscholarlist" onblur="nametoid(this)">
    <br/>
    

    源数组需要在函数之外。这是因为idtoname & nametoid不在$(function(){..})的范围内。所以他们将无法访问数组

    JS

       var scholaridlists = ["001", "002", "003", "004", "005", "006", "007", "008", "009", "010"];
       var scholarlists = ["Elnora", "Henry", "Scotty", "Bruna", "Shirley", "Modesto", "Lissa", "Davida", "Margherita", "Soo"];
       $(function() {
          // Rest of the code
       });
    
       function idtoname() {
         // rest of the code
       }
    
       function nametoid() {
         // rest of the code
       }
    

    DEMO

    【讨论】:

    • 哇,没想到解决办法这么简单。非常感谢@brk!无论如何,以前我确实尝试将阵列移到外面,但没有奏效。也许它与 onblur 与 onfocusout 有关?另外,出于好奇,如果您碰巧知道,为什么 idtoname() 在括号内没有“this”而不是 nametoid(this)?对不起,因为我对这个 JS/HTML 东西还很陌生。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2011-11-11
    • 1970-01-01
    • 2022-08-15
    • 2012-11-14
    相关资源
    最近更新 更多