2016-07-18 6 views
1

jQueryオブジェクトに値が含まれているかどうかを確認したいと思います。今jQuery Object Filled Or

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個以上のオブジェクトを持っています。 hasOwnProperty( "Email")){ アラート( "成功"); } if(objContactList.hasOwnProperty( "Address")){ alert( "success"); } –

+0

更新されたコードを確認してください。 –

+0

もう一度変更して、今すぐチェック –

0

こんにちはあなたはそうuがとても良くuを使用して確認することができます反復処理することはできませんそれぞれを使用してオブジェクトを使用して作成している作業、これを試してみてください2つの方法

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が複数のオブジェクトを持っているならば、

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 JSアレイに

を押してオブジェクトを確認する方法は確認する2つの方法が存在しています値が存在する

+0

ObjContactListの各メンバーに対してこれを行うにはどうすればいいですか?ObjContactListに30人以上のメンバーがいます –

+0

30個のオブジェクトを配列に入れてpuchする必要があります –

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/

関連する問題