2016-07-18 6 views
1

Я хочу проверить, содержит ли объект jQuery значение или нет.jQuery Object Filled or not

var objContactList = new Object(); 
objContacList.Address = "xyz abc"; 
objContactList.Email = "[email protected]"; 
objContactList.MobileNo = "9013027233"; 

теперь, я хочу, чтобы проверить, если ObjContactList.Address и objContactList.MobileNo заполнена или нет. Для этого я сделал это:

$.each(objContactList, function(i, val) { 
    if (objContactList[i].val() == '') { 
     // do this 
    } 
}) 
+0

То, что вы не является POJS объект - ничего общего с JQuery вообще. Кроме того, есть ли у вас проблемы с вашим кодом? –

+0

его объект, как и будет использовать каждый –

+0

да это не работает –

ответ

1

Попробуйте это его рабочая

var objContactList = new Object(); 
 
    objContactList.Address = "xyz abc"; 
 
    objContactList.Email = "[email protected]"; 
 
    objContactList.MobileNo = "9013027233"; 
 
    objContactList.pin = ""; 
 
    
 

 
for(var propertyName in objContactList) { 
 
    if(objContactList.hasOwnProperty(propertyName)){ 
 
    var value = objContactList[propertyName] 
 
    if(value == ""){ 
 
    console.log(propertyName + " has no value"); 
 
    } 
 
    else{ 
 
    console.log(propertyName + " has value"); 
 
    } 
 
     } 
 
    
 
    
 
}

+0

у меня есть более чем 30 объектов, как я могу сделать это для каждого если (objContactList. hasOwnProperty («Электронная почта»)) { предупреждение («успех»); } if (objContactList.hasOwnProperty («Адрес»)) { предупреждение («успех»); } –

+0

Проверьте обновленный код. –

+0

Сделано еще одно изменение, проверьте сейчас –

0

Привет вы создаете с помощью объекта так, используя каждый и не может перебирать, так что лучше и можно проверить с помощью два направления

1 Путь

var objContactList = new Object(); 
objContactList.Address = "xyz abc"; 
objContactList.Email = "[email protected]"; 
objContactList.MobileNo = "9013027233"; 

if(objContactList.hasOwnProperty("Address ")){ 
    alert("Address is there"); 
} 
if(objContactList.hasOwnProperty("Email")){ 
    alert("email is there"); 
} 

2way

var objContactList = new Object(); 
    objContactList.Address = "xyz abc"; 
    objContactList.Email = "[email protected]"; 
    objContactList.MobileNo = "9013027233"; 

    if(objContactList.Address != "" || objContactList.Address != undefined){ 
     alert("Address is there"); 
    } 

если и есть несколько объектов, то Нажмите на массив

JS

var myarray = []; 

var objContactList = new Object(); 
objContactList.Address = "xyz abc"; 
objContactList.Email = "[email protected]"; 
objContactList.MobileNo = "9013027233"; 

myarray.push(objContactList); 
var objContactList2 = new Object(); 
objContactList2.Address = "xyz abc"; 
objContactList2.Email = "[email protected]"; 
objContactList2.MobileNo = "9013027233"; 

myarray.push(objContactList2); 

for (var i = 0; i < myarray.length; i++) { 
    if (myarray[i].Address != "") { 
     alert("Address is there"); 
    } 
} 

1 способ проверить объект является существует 2 способа проверить имеется

+0

Как я могу это сделать для каждого члена в ObjContactList, у меня есть более 30 членов в ObjContactList –

+0

У вас есть, чтобы наложить все 30 объектов в массив –

0

попробуйте это:

var objContactList = new Object(); 
     objContactList.Address = "xyz abc"; 
     objContactList.Email = "[email protected]"; 
     objContactList.MobileNo = "9013027233"; 

     if (objContactList.hasOwnProperty('Address')) { 
      if (objContactList.Address != "") { 
       alert('Address is : ' + objContactList.Address); 
      } else { 
       alert(' Address is empty'); 
      } 
     } 
     if (objContactList.hasOwnProperty('Email')) { 
      if (objContactList.Email != "") { 
       alert('Email is : ' + objContactList.Email); 
      } else { 
       alert(' Email is empty'); 
      } 
     } 
     if (objContactList.hasOwnProperty('MobileNo')) { 
      if (objContactList.MobileNo != "") { 
       alert('MobileNo is : ' + objContactList.MobileNo); 
      } else { 
       alert(' MobileNo is empty'); 
      } 
     } 

https://jsfiddle.net/8apatfy6/