2010-04-08 5 views
3

I à filtrer sur la base de deux attributs, la première est id et la seconde est appelée niveaufiltre jQuery() - filtrage sur plusieurs attributs

ie id = niveau "someId" = "someLevel"

Ce code ne devrait-il pas faire l'affaire ... il ne semble pas fonctionner.

$(".someClass").filter("#"+id, "[level='"+level+"']").someAction(); 
+0

BTW: Je veux faire l'équivalent d'une SQL instruction AND pas une déclaration OU. – Ankur

Répondre

2

Vous fournissez deux arguments:

.filter("#someId", "[level='someLevel']") 

Vous voulez sans doute cela:

.filter("#someId[level=someLevel]") 
3

Qu'en est-

$(".someClass").filter("filter1").filter("filter2") 

?

0

Si son un ou condition que vous devriez vraiment avoir une aide function like string format:

string.Format = function(text) 
{ 
    //check if there are two arguments in the arguments list 
    if (arguments.length <= 1) 
    { 
     //if there are not 2 or more arguments there’s nothing to replace 
     //just return the original text 
     return text; 
    } 
    //decrement to move to the second argument in the array 
    var tokenCount = arguments.length - 2; 
    for(var token = 0; token <= tokenCount; token++) 
    { 
     //iterate through the tokens and replace their placeholders from the original text in order 
     text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), 
               arguments[ token + 1 ]); 
    } 
    return text; 
}; 

puis il est facile:

var filter = string.format("#{0}, [level='{1}']", id, level); 
$(".someClass").filter(filter).someAction();