0

非常感谢,斯特凡!但我尝试了第二种“替代方案”,但没有奏效。这是功能:

function filterData() {
   var searchString = document.getElementById('search-string').value.replace(/'/g, "\\'");
   var filter = document.getElementById('filter').value.replace(/'/g, "\\'");
    layer.setOptions({
      query: {
         select: "'geometry'",
         from: '4756019'
         where: "'" + filter + "' = '" + searchString + "'"
      }
   });
}
4

1 回答 1

1

我将您的代码放在 jsFiddle 上(经过我的更正):http: //jsfiddle.net/odi86/6jvG4/

您忘记将from子句添加到过滤器中,如果添加它就可以了:

if (filter === "UEU") {
    layer.setOptions({
        query: {
            select: "'geometry'",
            from: '4756019',
            where: "'UEU' = '" + searchString + "'"
        }
    });
}
else if (filter === "SUBUEU") {
    layer.setOptions({
        query: {
            select: "'geometry'",
            from: '4756019',
            where: "'SUBUEU' = '" + searchString + "'"
        }
    });
}
else if (filter === "CODIGO") {
    layer.setOptions({
        query: {
            select: "'geometry'",
            from: '4756019',
            where: "'CODIGO' = '" + searchString + "'"
        }
    });
}

顺便说一句:您可以通过这样做来大大简化您的代码(不是 if-else 必要的):

layer.setOptions({
        query: {
            select: "'geometry'",
            from: '4756019',
            where: "'" + filter + "' = '" + searchString + "'"
        }
    });
于 2012-08-02T10:27:10.347 回答