2013-03-27 11 views
15

Abbiamo una funzione chiamata .any in Prototipo. Voglio lo stesso come in Jquery.Qual è l'equivalente del prototipo .any in Jquery?

Il mio codice Prototype è:

if (item_name == '' || $R(1,ind).any(function(i){return($F("bill_details_"+i+"_narration") == item_name)})) { 
    alert("This item already added."); 
} 

voglio eseguire la funzione equivalente utilizzando jQuery.

Please help me per ottenere l'output desiderato. Grazie in anticipo ..

+1

se qualcuno sta dando downvote. per favore commenta il motivo .. –

+0

cosa c'è dentro 'ind' e' $ F'? – jantimon

+0

ind è un numero come il numero totale di righe. 1,2, ... –

risposta

23

Per IE 9 è costruito in:

Il alcuni() Test metodo se qualche elemento della matrice passa la prova realizzata dalla funzione prevista.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some

[2, 4, 6, 8, 10].some(function(n) { return n > 5; }); 
// -> true (the iterator will return true on 6) 

Per IE 8 e sotto:

Prototype any

[2, 4, 6, 8, 10].any(function(n) { return n > 5; }); 
// -> true (the iterator will return true on 6) 

È possibile utilizzare jQuery.grep:

jQuery.grep([2, 4, 6, 8, 10], function(n) { return n > 5; }).length > 0; 
// -> true (as grep returns [6, 8, 10]) 

sottolineatura _.any o _.some

_.any([2, 4, 6, 8, 10], function(n) { return n > 5; }); 
// -> true (the iterator will return true on 6) 
+0

puoi postare l'equivalente jquery per il mio codice prototipo di cui sopra. Per favore –

+0

'$ F' sta ancora usando Prototype, non jQuery. – Alnitak

+4

Per quanto riguarda le prestazioni però, fai attenzione che .any dovrebbe smettere di cercare dopo la prima occorrenza, mentre .grep attraverserà l'intero array. – Uri

0

Sembra che http://api.jquery.com/jQuery.grep/ sia quello che stai cercando.

prova

if (item_name == '' || $.grep([1,ind],function(i){return($("#bill_details_"+i+"_narration").attr("name") == item_name)}).length>0) { 
    alert("This item already added."); 
} 
+0

sono nuovo di Jquery. Quindi per favore dammi il codice equivalente :( –

5

ES5 ha una funzione incorporata chiamata Array.prototype.some che verifica riguarda se qualsiasi elemento in un array corrisponde una funzione predicato, e che ferma iterazione non appena un elemento corrispondente è trovato.

.some(function(el) { 
    return el.value === item_name; 
}); 

tuo problema allora diventa solo nel creare una matrice di elementi desiderati, che è più difficile di quanto sarebbe in prototipo perché non c'è operatore "range" in jQuery. Fortunatamente $.map itera oltre elementi vuoti, anche se il built-in Array.prototype.map non modo da poter utilizzare new Array(ind):

var found = $.map(new Array(ind), function(_, x) { 
    return "bill_details_" + (x + 1) + "_narration"; 
}).some(function(id) { 
    var el = document.getElementById(id); 
    return el && (el.value === item_name); 
}); 

Il collegamento include uno spessore per .some per browser meno recenti di cui sopra.

3

JQuery ha il metodo .is(), dalla documentazione: Check the current matched set of elements against a selector, element, or jQuery object and return *true* if at least one of these elements matches the given arguments. Così un codice equivalente è:

if (item_name == '' || $([1,ind]).is(function(i) { return $('#bill_details_'+i+'_narration').attr('name') == item_name; })) { 
     alert("This item already added."); 
} 
0

Secondo la documentazione di JQuery se si torna un falso nella funzione di callback si romperà il ciclo:

Siamo in grado di rompere il $.loop each() in una particolare iterazione rendendo la funzione di callback restituita falsa. Il ritorno non-falso è lo stesso di un'istruzione continue in un ciclo for; salterà immediatamente alla successiva iterazione.

Tratto da: http://api.jquery.com/jquery.each/

Problemi correlati