jQuery 自动完成 如何对两个不同的输入 id 文本框使用相同的本地数组?
我目前正在将其用于单个自动完成框(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 jQuery 调用中使用 Multiple Selector ,然后在其上调用 autocompleteArray :
You can use a Multiple Selector in your jQuery call and then call your autocompleteArray on that: