0

我正在对 xml 文件进行过滤搜索。到目前为止,如果我只选择一个属性,我已经成功地显示了正确的节点。Bur 当我想显示具有两个相同属性的节点时,我得到所有具有至少一个正确属性的节点。这是我的代码:

function loadFilter(xml){
    filterResults(xml);
}

function filterResults(xml)
{
    match=true;
    var candidate = $(xml).find("candidate");
    for (var i=0;i<candidate.length;i++)
    {
        var sex_key = $("#sex").val(); 
        var sex_value = $(candidate[i]).attr("sex");

        var eye_color = $("#eye-color").val();
        var eye_color_value = $(candidate[i]).attr("eyecolor");

        var transport = $("#transport").val();
        var transport_value = $(candidate[i]).attr("transport");

        var extra = $("#extra").val();
        var extra_value = $(candidate[i]).attr("extra");

        if( checkValue(sex_key, sex_value) == true)
        {
            match = true;
            displayFilteredCandidate();
        }

        if( checkValue(eye_color,eye_color_value) == true )
        {
            match = true;
            displayFilteredCandidate();
        }

        if( checkValue(transport,transport_value) == true )
        {
            match = true;
            displayFilteredCandidate();
        }

        if( checkValue(extra,extra_value) == true )
        {
            match = true;
            displayFilteredCandidate();
        }

        function displayFilteredCandidate(){
            console.log($(candidates[i].getElementsByTagName("img")[0]));
            var candidateName = $(candidates[i]).attr("name")+" "+$(candidates[i]).attr("surname");
            var candidateImage = $(candidates[i].getElementsByTagName("img")[0]).attr("src");
            var cand = CandidateThumb(candidateName,"../candidates/"+candidateImage,candidates[i]);
            cand.data({node:candidates[i]}).click(candidateClick);
            $("#mainContent").append("<div id='candidates'></div>");
            $("#candidates").append(cand);                  
        }
    }
}

function checkValue(key, value)
{
    var match = false;

    if(key == value)
    {
        match = true;
    } else {
        match = false;
    }
    console.log(match);
    return match;
}
4

2 回答 2

0

这是因为只要其中一个属性匹配,您就将 match 设置为 true。你要

if (
    checkValue(sex_key, sex_value) == true &&
    checkValue(eye_color,eye_color_value) == true &&
    checkValue(transport,transport_value) == true &&
    checkValue(extra,extra_value) == true
) {
    match = true;
    displayFilteredCandidate();
}

这样您就可以检查所有属性,并且仅在所有属性都匹配时才显示候选人。

编辑:如果你想匹配至少两个,就这样做

if (
    checkValue(sex_key, sex_value) +
    checkValue(eye_color,eye_color_value) +
    checkValue(transport,transport_value) +
    checkValue(extra,extra_value)
    >= 2
) {
    match = true;
    displayFilteredCandidate();
}

(这是建立在这样一个事实的基础上的,即如果您将布尔值与 + 相加,它们将变为整数(1 表示真,0 表示假)。)

于 2013-04-24T12:12:57.980 回答
0
function filterResults(xml)
{
    var numToMatch = 2;
    var matched = 0;
    var sex_key = $("#sex").val(); 
    var eye_color = $("#eye-color").val();
    var transport = $("#transport").val();
    var extra = $("#extra").val();
    var candidate = $(xml).find("candidate").each(function(){

    if(this.attr("sex") == sex_key)
        matched++;
    if(this.attr("eyecolor") == eye_color)
        matched++;
    if(this.attr("transport") == transport)
        matched++;
    if(this.attr("extra") == extra)
        matched++;
    if(matched >= numToMatch)
        displayFilteredCandidate();
}

我想如果你想使用你的比较功能并且因为我觉得它很酷,你可以这样做:

function filterResults(xml)
{
    var numToMatch = 2;
    var matched = 0;
    var sex_key = $("#sex").val(); 
    var eye_color = $("#eye-color").val();
    var transport = $("#transport").val();
    var extra = $("#extra").val();
    var candidate = $(xml).find("candidate").each(function(){

    matched += checkValue(this.attr("sex"), sex_key);
    matched += checkValue(this.attr("eyecolor"), eye_color);
    matched += checkValue(this.attr("transport"), transport);
    matched += checkValue(this.attr("extra"), extra);
    if(matched >= numToMatch)
        displayFilteredCandidate();
}
于 2013-04-24T12:16:57.233 回答