我正在对 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;
}