繁体   English   中英

用Java关联样式数组?

[英]Associative style arrays in Javascript?

我正在尝试以JS中关联数组的样式分配对象,但失败了,说“ task.id”未定义。 为什么是这样?

var response = Object();
$('.task-list').each(function() {
  response[this.id][$('#' + this.id).sortable('toArray')];
});

您将对象引用为二维数组。

您应该这样做:

var response = {};
$(".task-list").each(function () {
    response[this.id] = $(this).sortable('toArray');
}

另外,当您说错误是“ task.id未定义”时,您的意思是“ this.id未定义”吗? 如果要基于类选择元素,则它们可能没有明确的ID。

<span class="task-list">myTask</span>

您可能要添加一个ID:

<span class="task-list" id="myTask">myTask</span>

您正在尝试访问尚未创建的属性。 尽管目前尚不清楚您要从示例中尝试做什么。 我假设您想将response[this.id]的值设置为$('#' + this.id).sortable('toArray')

尝试以下方法:

var response = {};
$('.task-list').each(function() {
    response[this.id] = $(this).sortable('toArray');
});

也将其更改为使用$(this)代替$('#' + this.id)因为它更干净。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM