gpt4 book ai didi

javascript - JavaScript 中未定义错误的长度

转载 作者:行者123 更新时间:2023-11-28 05:46:06 25 4
gpt4 key购买 nike

从下面的代码中,如果我尝试控制台a.length,它会返回值6,但是如果我尝试将相同的值分配给其他变量或尝试返回 a.length 的值,它会抛出 'length' of undefined 错误。

function ResizeUI() {
var a = $("#ao-sales-histroy-grid").jqGrid('getGridParam', 'colNames');
console.log(a.length); //returns me 6
if ((a.length) < 10) { //returns error - 'length' of undefined
if ($("#ao-sales-histroy-container").width() !== 0) {
$('#ao-sales-histroy-grid').setGridWidth($("#ao-sales-histroy-container").width() - 10);
}

}
}

当我尝试记录a时,它返回我:

[
"Sku Department",
"Giveaways and Samples<br><input type='checkbox' na… onclick = 'SHRowcheckBox(this,event)' ></input>",
"Supplies<br><input type='checkbox' name='SHMatrixC… onclick = 'SHRowcheckBox(this,event)' ></input>",
"Displays<br><input type='checkbox' name='SHMatrixC… onclick = 'SHRowcheckBox(this,event)' ></input>",
"Magazines<br><input type='checkbox' name='SHMatrix… onclick = 'SHRowcheckBox(this,event)' ></input>",
"Total"
]

错误场景:

条件1:enter image description here

条件2:enter image description here

可能是一个小而愚蠢的错误,但请帮忙。提前致谢!!.

最佳答案

看来您没有从第一行 jquery 中获得正确的结果。

在使用 a.length 属性之前,您可以检查 a 是否为假或 null,如下所示:-

if(a && a.length) {
// do stuff here
}

但是您还需要调查 jq 返回的值是什么。

关于javascript - JavaScript 中未定义错误的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38505384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com