Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 比较用jquery加载的字符串_Javascript_Jquery - Fatal编程技术网

Javascript 比较用jquery加载的字符串

Javascript 比较用jquery加载的字符串,javascript,jquery,Javascript,Jquery,伙计们,我有下面的javascript函数,它应该检查表中一列的索引。问题是columnName和表中的名称的比较。 我使用columnName“ID”进行了测试,在表中找到了该列,但比较结果不正确,因此不会返回索引 我的代码: function getColumnIndex(columnName, grid) { console.log("loading column index for column: [" + columnName + "]"); $('div.hDivBo

伙计们,我有下面的javascript函数,它应该检查表中一列的索引。问题是columnName和表中的名称的比较。 我使用columnName“ID”进行了测试,在表中找到了该列,但比较结果不正确,因此不会返回索引

我的代码:

function getColumnIndex(columnName, grid) {
    console.log("loading column index for column: [" + columnName + "]");
    $('div.hDivBox > table > thead > tr > th', grid).each(function(index) {
         var name = $(this).attr('title');
         console.log("Checking cell: [" + name + "]");
         if (String(name) == columnName) {
              return index;
         }
    });
    console.log("no cell found with name: " + columnName);
    return -1;
} 
日志语句:

正在加载列[ID]的列索引
正在检查单元格:[ID]
正在检查单元格:[名称]
正在检查单元格:[说明]
正在检查单元格:[添加时间]
正在检查单元格:[UpdTime]
未找到名称为ID的单元格

由javascript函数分析的HTML示例:

<div class="hDivBox">
<table cellspacing="0" cellpadding="0">
    <thead>
        <tr>
            <th align="center" hidden="" axis="col0" title="ID" style="display: none;">
                <div style="text-align: center; width: 30px;">ID</div>
            </th>
            <th align="center" axis="col1" title="Name" class="">
                <div style="text-align: center; width: 250px;">Name</div>
            </th>
            <th align="center" axis="col2" title="Description" class="">
                <div style="text-align: center; width: 250px;">Beschreibung</div>
            </th>
            <th align="center" axis="col3" title="AddTime">
                <div style="text-align: center; width: 120px;">hinzugefügt</div>
            </th>
            <th align="center" axis="col4" title="UpdTime">
                <div style="text-align: center; width: 120px;">aktualisiert</div>
            </th>
        </tr>
    </thead>
</table>

身份证件
名称
贝施雷邦
欣祖格特
阿库阿利谢特

只需使用a来匹配标题并使用,而不是自己循环

function getIndexByTitle( title ) {
 return $('th[title="' + title + '"]').index();
}

alert(getIndexByTitle("ID"));
alert(getIndexByTitle("Name"));

传递给
的匿名函数中的
return
语句。每个()
jQuery函数只从该函数返回,它也不会从
getColumnIndex()
函数返回

相反,我将执行以下操作:

function getColumnIndex(columnName, grid) {
    console.log("loading column index for column: [" + columnName + "]");
    var index = -1;
    $('div.hDivBox > table > thead > tr > th', grid).each(function(i) {
        var name = $(this).attr('title');
        console.log("Checking cell: [" + name + "]");
        if (String(name) == columnName) {
            index = i;
            return;
        }
    });
    if(index == -1)
        console.log("no cell found with name: " + columnName);
    return index;
}
基本原则是,您只需将正确的索引存储在作用域在匿名函数之外的变量中,而不是从匿名函数返回索引,这样您就可以在
调用完成后访问它。

您正在与函数一起使用

.each(function(){
    ...
    return index;
});
return -1;
这将从回调返回(如果
index
false
,则可能停止每个循环),但决不会从外部
getColumnIndex
函数中断并返回!因此,该函数将始终返回
-1

快速修复:

function getColumnIndex(columnTitle, grid) {
    var index = -1;
    $('div.hDivBox > table > thead > tr:first > th', grid).each(function(i) {
        if ($(this).attr('title') == columnTitle) {
            index = i;
            return false; // break each-loop
        }
    });
    return index;
}

您将从
每次迭代中调用的内部函数返回。您需要在每次调用之外保存索引,以便以后访问它

function getColumnIndex(columnName, grid) {
    var columnIndex;
    console.log("loading column index for column: [" + columnName + "]");
    $('div.hDivBox > table > thead > tr > th', grid).each(function(index) {
         var name = $(this).attr('title');
         console.log("Checking cell: [" + name + "]");
         if (String(name) == columnName) {
              columnIndex = index;
              // return false to exit the each iteration early
              return false;
         }
    });

    if (typeof columnIndex !== "undefined") {
        return columnIndex;
    };

    console.log("no cell found with name: " + columnName);
    return -1;
}

您不需要强制转换
String(name)
,因为它已经是一个字符串了。另外,当你谈论ID、姓名和头衔时,一定要确保它们都是不同的属性!奇怪的为了以防万一,我会尝试
String(name)==String(columnName)
,尽管我怀疑这会改变任何可能重复的东西,因为我不知道该函数。感谢您提供此智能解决方案;)