我正在搜索xml文件的过滤器。到目前为止,如果我只选择一个属性,我已经成功显示了正确的节点。当我想显示具有两个相同属性的节点时,我得到所有具有至少一个right属性的节点。继承我的代码:
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;
}
答案 0 :(得分:0)
这是因为只要其中一个属性匹配,就将匹配设置为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表示假)。)
答案 1 :(得分: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();
}