Possible Duplicate:
jQuery variable claiming it's undefined when it has been defined
问题在于,在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
最佳答案
问题出在您的findFirmware()
函数中,将其更改为
function findFirmware(li) {
if( li == null ) return alert("No match!");
firmware = li.selectFirmware;
firmwareid = phone.replace(".","");
$(".info").hide();
$('#' + phoneid + firmwareid).show(); // This line was messed up
};
该行
$(phoneid+firmware).show
存在两个问题,如果您计算缺少的括号和分号,那么还有四个问题,但是...您尝试显示的div有一个ID,但您的选择器中没有
#
即可通过ID选择元素firmware
包含带句点的未解析字符串,因此当div ID为phoneid + firmware
时iphone2g1.2
变为iphone2g12
,因此您需要使用从中解析出firmwareid
的字符串。小提琴演示:http://jsfiddle.net/AaNWM/
关于javascript - 自动填充jQuery插件无法正常工作,声称变量未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8232869/