自动完成 jQuery 插件不起作用,声称变量未定义

发布于 2024-12-17 12:31:16 字数 2304 浏览 2 评论 0原文

可能的重复:
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 技术交流群。

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

发布评论

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

评论(3

梦中楼上月下 2024-12-24 12:31:16

问题出在您的 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
};

There有两个问题 $(phoneid+firmware).show,如果算上缺少的括号,则有四个问题和分号但是...

  1. 您尝试显示的 div 有一个 ID,您的选择器中没有 # 来通过 ID 选择元素
  2. firmware 包含未解析的字符串加上句点 phoneid当您的 div ID 为 iphone2g12 时,固件 变为 iphone2g1.2,因此您需要使用将其解析出来的 firmwareid

小提琴演示:http://jsfiddle.net/AaNWM/

The problem is in your findFirmware() function, change it to

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
};

There two problems with this line $(phoneid+firmware).show, well four if you count the missing parenthesis and semicolon but...

  1. The div your trying to show has an ID, you don't have # in your selector to select the element by ID
  2. firmware contains the unparsed string with the period so phoneid + firmware becomes iphone2g1.2 when your div ID is iphone2g12 thus you needed to use firmwareid in which you parsed it out of.

Fiddle Demo: http://jsfiddle.net/AaNWM/

从来不烧饼 2024-12-24 12:31:16

它未定义,因为您这样做:

li.selectPhone;

传入的 li 元素上不存在 Which .selectPhone 。因此,一旦您开始替换字符串,就会收到错误。

假设我可能认为我可能知道您想要做什么,我更改了li.selectPhoneli.selectFirmware到 li.innerHTML ,它工作正常。 jsFiddle

编辑:您还在固件函数中使用了phone.replace,所以我也在上面的小提琴中更改了它。

Its undefined because you do:

li.selectPhone;

Which .selectPhone doesn't exist on the li 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 and li.selectFirmware to li.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.

剑心龙吟 2024-12-24 12:31:16

phone 未定义,因为 li.selectPhone 未定义。

如果没有看到 autoCompleteArray(),很难猜出为什么缺少它,乍一看,它似乎不是 jQuery 自动完成插件的一部分?

phone is undefined because li.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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文