2016-07-18 27 views
1

我想檢查jQuery對象是否包含值。jQuery對象是否被填充

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

現在,我要檢查,如果ObjContactList.AddressobjContactList.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個對象我如何做到這一點每個 if(objContactList。 hasOwnProperty(「Email」)){ alert(「success」); } if(objContactList.hasOwnProperty(「Address」)){ alert(「success」); } –

+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"); 
    } 

如果u有多個對象,然後推到陣列

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

你必須puch整個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/