0

有人可以帮我解决这个问题吗?

我从另一个程序员那里得到了这个功能:

function checkRequired(fieldList) {
    var field = null;
    var blankFields = [];
    for (var i = 0; i < fieldList.length; i++) {
        listItem = fieldList[i].split;
        fieldId = field[0];
        label = field[1];
        field = document.getElementById(fieldList[i].split("||")[0]);
        if (isVisible(field) && isBlank(field)) {
            blankFields[blankFields.length] = fieldList[i].split[1];
        }
    }

我将这个 json 数组传递给它:

var fieldList = new Array("'15||Project Requested By'","'18||Project Title'","'20||Banner Details'","'202||Flyer Details'","'2134||If Gl Code Not above'","'5862||Quantity'");

使用 FB 我看到函数在这一行出错了[listItem = fieldList[i].split;]

我不完全了解 .split 正在做什么以及它引起的问题。

任何帮助,将不胜感激。

更新:添加了显示错误发生位置的图像: 在此处输入图像描述

4

2 回答 2

1

You need to provide split with a parameter that tells it what to split on.

Right now, you're simply assigning the split function itself to listItem; you're not calling it.

You probably mean

listItem = fieldList[i].split("||");

MDN Docs for split

EDIT

On further inspection, you're not assigning anything to field, so it's equal to null at the point that you try to access it like an array. Thus, what you probably mean is:

 field = fieldList[i].split("||");
于 2013-01-17T17:30:57.360 回答
0

我的猜测是函数应该是这样的:

function checkRequired(fieldList) {
    var field = null,
        blankFields = [],
        listText, listItem, fieldId, label;

    for (var i = 0; i < fieldList.length; i++) {
        listText = fieldList[i];
        listText = listText.substring(1, listText.length - 1); // trim ' (single quote)
        listItem = listText.split("||");
        fieldId = listItem[0];
        label = listItem[1];
        field = document.getElementById(fieldId);

        if (isVisible(field) && isBlank(field)) {
            blankFields.push(label);
        }
    }

    return blankFields;
}

假设 isVisible() 和 isBlank() 在某处定义。

于 2013-01-17T17:52:52.263 回答