【发布时间】:2014-09-28 17:55:50
【问题描述】:
我有一个包含多个数组的 javascript 对象。我使用classes 和click events 来识别被点击的内容,并使用对象告诉DOM 显示什么。
有问题的对象看起来像这样(简化):
var inputSelection = {
account : ["registration","password"],
device : ["forceStop","clearData"]
};
让我们假设我们有这样的 HTML:
<div id="reasonDiv"><fieldset><legend>Reason</legend>
<label class="account"><input type="checkbox">Account<br /></label>
<label class="device"><input type="checkbox" />Device<br />
</div>
<div id="context"><fieldset><legend>Context</legend>
<label class="clearData"><input type="checkbox">Clear Data<br /></label>
<label class="forceStop"><input type="checkbox" />Force Stop<br />
<label class="registration"><input type="checkbox">Registration<br /></label>
<label class="password"><input type="checkbox" />Password<br /></label></fieldset>
</div>
本质上,我希望能够在单击 Account 时找到对象的 account 数组中的所有值。
当点击设备时,也可以访问device数组中的值。
我试过这个特殊的代码:
$('#reasonDiv label').on('click', function(){
node1 = $(this).attr('class');
$.each( inputSelection[node1], function( intValue, currentElement ) {
$('div#context').each('label', function(){
if($(this).attr('class') === currentElement){
$(this).show();
}
});
});
});
我认为我只是错误地嵌套了 each 函数,因为我在 console 中不断收到这个奇怪的错误说:
TypeError: c.apply is not a function @ jquery.min.js:2
目标是单击一个复选框,并显示关联数组中包含 any 类的所有项目。
我在这里错过了什么?
【问题讨论】:
-
jQuery 文件中的错误通常意味着您编写的代码中有语法错误。
-
是的,当我嵌套两个 .each 语句时,回调似乎失败了。我只是似乎无法找到在哪里确定它......
-
我建议始终关闭您的 HTML 标记以便于故障排除和阅读(因为它让代码格式化程序可以做他们的事情)
标签: jquery arrays class loops object