2016-11-30 12 views
5
var selector = '#Id-value_' + index; 
var exist = $(selector).exists(); 

Dostaję błąd dla tego fragmentu kodu.
Mój dokument gotowy funkcja jest

$(document).ready(function() { 

}); 

Odpowiedz

6

Nie ma exists() funkcja w jQuery. Ale można szybko napisać:

//add a new function to jQuery 
jQuery.fn.exists = function(){return this.length>0;} 

//now let's test it 
if ($(selector).exists()) { 
    // Do something 
} 
3

podstawie @ K48 odpowiedź, można również bezpośrednio sprawdzić, czy istnieje ona za pomocą $(selector).length.

var selector = '#Id-value_' + index; 
if ($(selector).length) { 
    // Do something 
} 
Powiązane problemy