jQuery 自动完成 如何对两个不同的输入 id 文本框使用相同的本地数组?

发布于 2024-09-27 03:14:29 字数 1161 浏览 1 评论 0原文

我目前正在将其用于单个自动完成框(id =“drug”),并且效果非常好。我需要有第二个自动完成框(id =“drug2”),它使用相同的数组作为其选择。我似乎根本无法让它发挥作用。每次我尝试修改代码时,我都会破坏两者。我确信有人有一个优雅的解决方案,可以引导我走向正确的方向。 。 。

<script type="text/javascript">
function findValue(li) {
 if( li == null ) return alert("No match!");

 // if coming from an AJAX call, let's use the DrugId as the value
 if( !!li.extra ) var sValue = li.extra[0];

 // otherwise, let's just display the value in the text box
 else var sValue = li.selectValue;

 //alert("The value you selected was: " + sValue);
}

function selectItem(li) {
 findValue(li);
}

function formatItem(row) {
 return row[0] + " (id: " + row[1] + ")";
}

function lookupLocal(){
 var oSuggest = $("#drug")[0].autocompleter;
 oSuggest.findValue();
 return false;
}

$(document).ready(function() {
 $("#drug").autocompleteArray(

  ['Acyclovir','Amikacin','Aminophylline','Amiodarone','Ampicillin','Atropine','Azithromycin','Aztreonam','Bupivacaine','Calcium Chloride','Calcium'],
  {
   delay:10,
   minChars:1,
   matchSubset:1,
   onItemSelect:selectItem,
   onFindValue:findValue,
   autoFill:true,
   maxItemsToShow:100
  }

 );
});
</script>

I am currently using this for a single autocomplete box (id="drug"), and it works beautifully. I need to have a second autocomplete box (id="drug2") that uses the same array as its choices. I cannot seem to get it to work at all. Every time I try to modify the code I break it on the both. I'm sure someone out there has an elegant solution that will lead me in the right direction . . .

<script type="text/javascript">
function findValue(li) {
 if( li == null ) return alert("No match!");

 // if coming from an AJAX call, let's use the DrugId as the value
 if( !!li.extra ) var sValue = li.extra[0];

 // otherwise, let's just display the value in the text box
 else var sValue = li.selectValue;

 //alert("The value you selected was: " + sValue);
}

function selectItem(li) {
 findValue(li);
}

function formatItem(row) {
 return row[0] + " (id: " + row[1] + ")";
}

function lookupLocal(){
 var oSuggest = $("#drug")[0].autocompleter;
 oSuggest.findValue();
 return false;
}

$(document).ready(function() {
 $("#drug").autocompleteArray(

  ['Acyclovir','Amikacin','Aminophylline','Amiodarone','Ampicillin','Atropine','Azithromycin','Aztreonam','Bupivacaine','Calcium Chloride','Calcium'],
  {
   delay:10,
   minChars:1,
   matchSubset:1,
   onItemSelect:selectItem,
   onFindValue:findValue,
   autoFill:true,
   maxItemsToShow:100
  }

 );
});
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2024-10-04 03:14:29

您可以在 jQuery 调用中使用 Multiple Selector ,然后在其上调用 autocompleteArray :

$(document).ready(function() {
 $("#drug1, #drug2").autocompleteArray(
  ['Acyclovir','Amikacin','Aminophylline','Amiodarone','Ampicillin','Atropine','Azithromycin','Aztreonam','Bupivacaine','Calcium Chloride','Calcium'],
  {
   delay:10,
   minChars:1,
   matchSubset:1,
   onItemSelect:selectItem,
   onFindValue:findValue,
   autoFill:true,
   maxItemsToShow:100
  }    
 );
});

You can use a Multiple Selector in your jQuery call and then call your autocompleteArray on that:

$(document).ready(function() {
 $("#drug1, #drug2").autocompleteArray(
  ['Acyclovir','Amikacin','Aminophylline','Amiodarone','Ampicillin','Atropine','Azithromycin','Aztreonam','Bupivacaine','Calcium Chloride','Calcium'],
  {
   delay:10,
   minChars:1,
   matchSubset:1,
   onItemSelect:selectItem,
   onFindValue:findValue,
   autoFill:true,
   maxItemsToShow:100
  }    
 );
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文