jQuery的变量时,声称已经将它定义它的不确定 [英] jQuery variable claiming it's undefined when it has been defined

查看:120
本文介绍了jQuery的变量时,声称已经将它定义它的不确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有两个文本框自动填充,一个用于手机型号 - 输入1和一个用于固件 - 在同一页面上输入2。当两个装满我想用ID input1input2显示一个div,但在输入1输入一个值时,它声称对手机的变量是不确定的,而在第二次填充时声称phoneid是不确定的。
这里的HTML

 < D​​IV ID =formcontainer>
<输入ID =输入1/>
<输入ID =输入2/>
< / DIV>
< D​​IV ID =iphone2g1.1级=信息的风格=显示:无> iPhone 2G< / DIV>
< D​​IV ID =iphone2g1.2级=信息的风格=显示:无> iPhone 3G< / DIV>
< D​​IV ID =iphone2g1.3级=信息的风格=显示:无>的iPhone 3GS< / DIV>
< D​​IV ID =iphone2g1.4级=信息的风格=显示:无> iPhone 4℃; / DIV>
< D​​IV ID =iphone2g1.5级=信息的风格=显示:无> iPhone 4S的< / DIV>

jQuery的

  $(#输入1)。autocompleteArray([iPhone 2G,iPhone的3G,iPhone 3GS,iPhone 4,iPhone 4S]
    {minChars:1,
        matchSubset:1,
        onItemSelect:selectPhone,
        onFindValue:findPhone,
        自动填充:真实,
        maxItemsToShow:10,
        selectFirst:真实,
    });$(#输入2)。autocompleteArray([1.1,1.2,1.3,1.4,1.5]
{minChars:1,
    matchSubset:1,
    onItemSelect:selectFirmware,
    onFindValue:findFirmware,
    自动填充:真实,
    maxItemsToShow:10,
    selectFirst:真实,
    });功能findPhone(LI){
    如果(李== NULL)回报警报(不匹配!);
    VAR手机= li.selectPhone;
    VAR phoneid = phone.replace(iPhone,iphone)与toLowerCase()。
};功能findFirmware(LI){
    如果(李== NULL)回报警报(不匹配!);
    VAR固件= li.selectFirmware;
    $(信息)。隐藏
    $(phoneid +固件).show
};功能selectPhone(LI){
    findPhone(LI);
}功能selectFirmware(LI){
    findFirmware(LI);
}

我使用的自动完成插件。
该页面可以查看 rel=\"nofollow\">。

感谢。

EDIT1 这是什么现在jQuery的样子,但仍然抛出了同样的错误。

  VAR手机;
    VAR phoneid;
    VAR固件;
    VAR firmwareid;$(#输入1)。autocompleteArray([iPhone 2G,iPhone的3G,iPhone 3GS,iPhone 4,iPhone 4S]
{minChars:1,
    matchSubset:1,
    onItemSelect:selectPhone,
    onFindValue:findPhone,
    自动填充:真实,
    maxItemsToShow:10,
    selectFirst:真实,
});$(#输入2)。autocompleteArray([1.1,1.2,1.3,1.4,1.5]
{minChars:1,
    matchSubset:1,
    onItemSelect:selectFirmware,
    onFindValue:findFirmware,
    自动填充:真实,
    maxItemsToShow:10,
    selectFirst:真实,
    });功能findPhone(LI){
    如果(李== NULL)回报警报(不匹配!);
    手机= li.selectPhone;
    phoneid = phone.replace(iPhone,iphone)与toLowerCase()。
};功能findFirmware(LI){
    如果(李== NULL)回报警报(不匹配!);
    固件= li.selectFirmware;
    firmwareid = phone.replace(,。);
    $(信息)。隐藏
    $(phoneid +固件).show
};功能selectPhone(LI){
    findPhone(LI);
}功能selectFirmware(LI){
    findFirmware(LI);
}


问题是在你的div的ID属性,并在 findFirmware()函数周期,改变它到

 函数findFirmware(LI){
    如果(李== NULL)回报警报(不匹配!);
    固件= li.selectFirmware;
    firmwareid = phone.replace(,。);
    $(信息),隐藏()。
    $('#'+ phoneid + firmwareid).show(); //这一行如鱼得水
};

有两个问题行 $(phoneid +固件).show ,以及4如果算上缺少括号和分号,但...


  1. 您想显示的div有一个ID,您不必在你的选择由ID来选择元素

  2. 固件包含未解析字符串以期让 phoneid +固件变成 iphone2g1。 2 当你的DIV ID是 iphone2g12 因此,你需要使用 firmwareid 中,你解析出来的。

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

I'm trying to have two autofilling textboxes, one for a phone model - input1 and one for firmware - input2 on the same page. When both filled I want a div to be shown with the ID input1input2, but when entering a value in input1 it claims the variable for phone is undefined, and when filling in the second it claims phoneid is undefined. Here's the HTML

<div id="formcontainer">
<input id="input1"/>
<input id="input2"/>
</div>
<div id="iphone2g1.1" class="info" style="display:none">iPhone 2G</div>
<div id="iphone2g1.2" class="info" style="display:none">iPhone 3G</div>
<div id="iphone2g1.3" class="info" style="display:none">iPhone 3GS</div>
<div id="iphone2g1.4" class="info" style="display:none">iPhone 4</div>
<div id="iphone2g1.5" class="info" style="display:none">iPhone 4S</div>

jQuery

$("#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!");
    var phone = li.selectPhone;
    var phoneid = phone.replace("iPhone ","iphone").toLowerCase();
};

function findFirmware(li) {
    if( li == null ) return alert("No match!");
    var firmware = li.selectFirmware;
    $(".info").hide
    $(phoneid+firmware).show
};

function selectPhone(li) {
    findPhone(li);
}

function selectFirmware(li) {
    findFirmware(li);
}

I'm using this for the autocomplete plugin. The page can be viewed here.

Thanks.

EDIT1 This is now what the jQuery looks like, but it still throws up the same error.

    var phone;
    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);
}

解决方案

The problem is the periods in your div's ID attributes and 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/

这篇关于jQuery的变量时,声称已经将它定义它的不确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆