【发布时间】:2012-09-21 16:00:15
【问题描述】:
我正在尝试将值添加到一个简单的数组中,但我无法将值推送到数组中。
到目前为止一切顺利,这是我拥有的代码:
codeList = [];
jQuery('a').live(
'click',
function()
{
var code = jQuery(this).attr('id');
if( !jQuery.inArray( code, codeList ) ) {
codeList.push( code );
// some specific operation in the application
}
}
);
上面的代码不起作用! 但是如果我手动传递值:
codeList = [];
jQuery('a').live(
'click',
function()
{
var code = '123456-001'; // CHANGES HERE
if( !jQuery.inArray( code, codeList ) ) {
codeList.push( code );
// some specific operation in the application
}
}
);
有效!
我无法弄清楚这里发生了什么,因为如果我手动进行其他测试,它也可以工作!
【问题讨论】:
-
包含 HTML。我很确定这就是问题所在。
-
如果您使用的是 XHTML 或 HTML5 之前的文档类型,则以数字开头的 ID 无效。
标签: javascript jquery arrays push