0

我正在使用toastr并希望使用从 ajax 调用返回的 json 对象设置选项。我在动态设置选项属性和值时遇到了一些麻烦。这是示例代码:

if(data.toast){
    $.each(data.toast, function(index, element) {
    toastr.options.index = element;
    });
}

data.toast 是一个 json 对象,如下所示:

"toast":{"closeButton":true}

如果我硬编码它会是什么样子:

toastr.options.closeButton = true;

我应该将迭代器中的 .index 更改为什么,以便它将其评估为变量

4

1 回答 1

1
if(data.toast){
    $.each(data.toast, function(index, element) {
    toastr.options[index] = element;
    });
}

这样做是为了options.index尝试访问index不是索引值的属性的属性。

上述方法将解决该问题。它将对象视为一种关联数组。所以toastr.options[index]评估toastr.options["closeButton"]与 相同toastr.options.closeButton

于 2015-07-09T21:00:51.250 回答