自动完成 jQuery 插件不起作用,声称变量未定义
可能的重复:
jQuery 变量在定义时声明其未定义
问题是,当在 Firefox 中打开它并为输入 1 键入一个值并选择它时,Firebug 会说变量 Phone 未定义。我尝试在开始时定义变量,但它仍然不起作用。
这是 jQuery:
$(document).ready(function() {
var phone; //fix scoping
var phoneid;
var firmware;
var firmwareid;
$("#input1").autocompleteArray(["iPhone 2G","iPhone 3G","iPhone 3GS","iPhone 4","iPhone 4s"],
{ minChars:1,
matchSubset:1,
onItemSelect:selectPhone,
onFindValue:findPhone,
autoFill:true,
maxItemsToShow:10,
selectFirst:true,
});
$("#input2").autocompleteArray(["1.1","1.2","1.3","1.4","1.5"],
{ minChars:1,
matchSubset:1,
onItemSelect:selectFirmware,
onFindValue:findFirmware,
autoFill:true,
maxItemsToShow:10,
selectFirst:true,
});
function findPhone(li) {
if( li == null ) return alert("No match!");
phone = li.selectPhone;
phoneid = phone.replace("iPhone ","iphone").toLowerCase();
};
function findFirmware(li) {
if( li == null ) return alert("No match!");
firmware = li.selectFirmware;
firmwareid = phone.replace(".","");
$(".info").hide
$(phoneid+firmware).show
};
function selectPhone(li) {
findPhone(li);
}
function selectFirmware(li) {
findFirmware(li);
}
});
这是 HTML:
<div id="formcontainer">
<input id="input1"/>
<input id="input2"/>
</div>
<div id="iphone2g11" class="info" style="display:none">iPhone 2G</div>
<div id="iphone2g12" class="info" style="display:none">iPhone 3G</div>
<div id="iphone2g13" class="info" style="display:none">iPhone 3GS</div>
<div id="iphone2g14" class="info" style="display:none">iPhone 4</div>
<div id="iphone2g15" class="info" style="display:none">iPhone 4S</div>
我正在使用此插件进行自动完成 http ://www.pengoworks.com/workshop/jquery/autocomplete.htm
Possible Duplicate:
jQuery variable claiming it's undefined when it has been defined
The problem is that when opening it in firefox and typing a value for input 1 and selecting it, firebug says that the variable phone isn't defined. I tried defining the variables at the start but it still ceased to work.
This is the jQuery:
$(document).ready(function() {
var phone; //fix scoping
var phoneid;
var firmware;
var firmwareid;
$("#input1").autocompleteArray(["iPhone 2G","iPhone 3G","iPhone 3GS","iPhone 4","iPhone 4s"],
{ minChars:1,
matchSubset:1,
onItemSelect:selectPhone,
onFindValue:findPhone,
autoFill:true,
maxItemsToShow:10,
selectFirst:true,
});
$("#input2").autocompleteArray(["1.1","1.2","1.3","1.4","1.5"],
{ minChars:1,
matchSubset:1,
onItemSelect:selectFirmware,
onFindValue:findFirmware,
autoFill:true,
maxItemsToShow:10,
selectFirst:true,
});
function findPhone(li) {
if( li == null ) return alert("No match!");
phone = li.selectPhone;
phoneid = phone.replace("iPhone ","iphone").toLowerCase();
};
function findFirmware(li) {
if( li == null ) return alert("No match!");
firmware = li.selectFirmware;
firmwareid = phone.replace(".","");
$(".info").hide
$(phoneid+firmware).show
};
function selectPhone(li) {
findPhone(li);
}
function selectFirmware(li) {
findFirmware(li);
}
});
And this is the HTML:
<div id="formcontainer">
<input id="input1"/>
<input id="input2"/>
</div>
<div id="iphone2g11" class="info" style="display:none">iPhone 2G</div>
<div id="iphone2g12" class="info" style="display:none">iPhone 3G</div>
<div id="iphone2g13" class="info" style="display:none">iPhone 3GS</div>
<div id="iphone2g14" class="info" style="display:none">iPhone 4</div>
<div id="iphone2g15" class="info" style="display:none">iPhone 4S</div>
I'm using this plugin for autocomplete http://www.pengoworks.com/workshop/jquery/autocomplete.htm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题出在您的
findFirmware()
函数中,将其更改为There有两个问题
$(phoneid+firmware).show
,如果算上缺少的括号,则有四个问题和分号但是...#
来通过 ID 选择元素firmware
包含未解析的字符串加上句点phoneid当您的 div ID 为
变为iphone2g12
时,固件iphone2g1.2
,因此您需要使用将其解析出来的firmwareid
。小提琴演示:http://jsfiddle.net/AaNWM/
The problem is in your
findFirmware()
function, change it toThere two problems with this line
$(phoneid+firmware).show
, well four if you count the missing parenthesis and semicolon but...#
in your selector to select the element by IDfirmware
contains the unparsed string with the period sophoneid + firmware
becomesiphone2g1.2
when your div ID isiphone2g12
thus you needed to usefirmwareid
in which you parsed it out of.Fiddle Demo: http://jsfiddle.net/AaNWM/
它未定义,因为您这样做:
传入的
li
元素上不存在 Which.selectPhone
。因此,一旦您开始替换字符串,就会收到错误。假设我可能认为我可能知道您想要做什么,我更改了
li.selectPhone
和li.selectFirmware
到 li.innerHTML ,它工作正常。 jsFiddle。编辑:您还在固件函数中使用了
phone.replace
,所以我也在上面的小提琴中更改了它。Its undefined because you do:
Which
.selectPhone
doesn't exist on theli
element passed in. So, once you get to replacing the string, you get an error.Assuming that I may, possibly, think, that I might know what you are trying to do, I changed
li.selectPhone
andli.selectFirmware
toli.innerHTML
and it works fine. jsFiddle.Edit: You also were using
phone.replace
in your firmware function so I changed that as well in the above fiddle.phone
未定义,因为li.selectPhone
未定义。如果没有看到 autoCompleteArray(),很难猜出为什么缺少它,乍一看,它似乎不是 jQuery 自动完成插件的一部分?
phone
is undefined becauseli.selectPhone
is undefined.It's hard to guess why that's missing without seeing autoCompleteArray(), which doesn't seem to be part of the jQuery autocomplete plugin, at first glance?