2017-03-27 81 views
-1

jQuery代碼:如何將元素添加到jQuery中的現有對象?

var addproperty = { 
    OwnerName: OwnerName, 
    OwnerEmail: OwnerEmail, 
    OwnerMobile: OwnerMobile, 
    OwnerType: OwnerType, 
    Propertyfor: Propertyfor, 
    ProjectName: ProjectName, 
    ProjectAddress: ProjectAddress, 
    State: State, 
    City: City, 
    Area: Area, 
    ProjectType: ProjectType, 
    BHK: BHK, 
    Bathroom: Bathroom, 
    Balcony: Balcony, 
    Builtup: Builtup, 
    Carpet: Carpet, 
    Unit: Unit, 
    Furnish: Furnish 
} 

if(Propertyfor == 'rent'){ 
    $(addproperty).extend({ 
     RentPossessionDate: RentPossessionDate, 
     MonthlyRent: MonthlyRent, 
     Maintanace: Maintanace, 
     SecuiryDeposit: SecuiryDeposit, 
     Bachelors: Bachelors, 
     RentNegotiable: RentNegotiable 
}); 

它不插入新元素插入現有的對象與addproperty。我的編碼錯誤在哪裏?

回答

2

這個問題是因爲您提供了addproperty對象給jQuery對象的構造函數。

要使用extend()正確,只是用從jQuery對象直接,即$.extend(obj, newObj)調用它,就像這樣:

var addproperty = { 
 
    OwnerName: 'OwnerName', 
 
    OwnerEmail: 'OwnerEmail', 
 
    OwnerMobile: 'OwnerMobile', 
 
    OwnerType: 'OwnerType', 
 
    Propertyfor: 'Propertyfor', 
 
    ProjectName: 'ProjectName', 
 
    ProjectAddress: 'ProjectAddress', 
 
    State: 'State', 
 
    City: 'City', 
 
    Area: 'Area', 
 
    ProjectType: 'ProjectType', 
 
    BHK: 'BHK', 
 
    Bathroom: 'Bathroom', 
 
    Balcony: 'Balcony', 
 
    Builtup: 'Builtup', 
 
    Carpet: 'Carpet', 
 
    Unit: 'Unit', 
 
    Furnish: 'Furnish' 
 
} 
 

 
var Propertyfor = 'rent'; 
 

 
if (Propertyfor == 'rent') { 
 
    $.extend(addproperty, { 
 
    RentPossessionDate: 'RentPossessionDate', 
 
    MonthlyRent: 'MonthlyRent', 
 
    Maintanace: 'Maintanace', 
 
    SecuiryDeposit: 'SecuiryDeposit', 
 
    Bachelors: 'Bachelors', 
 
    RentNegotiable: 'RentNegotiable' 
 
    }); 
 
} 
 

 
console.log(addproperty);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

注意,對於上述工作,我不得不改變屬性值轉換爲字符串,因爲您沒有提供代碼庫的那部分內容。

+0

沒問題老兄字符串轉換,但現在它的工作,我會接受6分鐘 –

+0

不客氣內你的答案,很樂意幫助 –

0

我在代碼中修復了一些問題。有很多拼寫錯誤。函數必須與您想要擴展的對象一起提供,並且還需要包含您需要的屬性的新對象。

var addProperty = { 
    OwnerName: 'OwnerName', 
    OwnerEmail: 'OwnerEmail', 
    OwnerMobile: 'OwnerMobile', 
    OwnerType: 'OwnerType', 
    Propertyfor: 'rent', 
    ProjectName: 'ProjectName', 
    ProjectAddress: 'ProjectAddress', 
    State: 'State', 
    City: 'City', 
    Area: 'Area', 
    ProjectType: 'ProjectType', 
    BHK: 'BHK', 
    Bathroom: 'Bathroom', 
    Balcony: 'Balcony', 
    Builtup: 'Builtup', 
    Carpet: 'Carpet', 
    Unit: 'Unit', 
    Furnish: 'Furnish' 
}; 

if (addProperty.Propertyfor == 'rent') { 
    var obj = { RentPossessionDate: 'RentPossessionDate', 
       MonthlyRent: 'MonthlyRent', 
       Maintanace: 'Maintanace', 
       SecuiryDeposit: 'SecuiryDeposit', 
       Bachelors: 'Bachelors', 
       RentNegotiable: 'RentNegotiable' 
       }; 
    $.extend(addProperty, obj); 
    console.log(addProperty); 
} 

觀察控制檯。這裏有一個工作小提琴: https://jsfiddle.net/fqtaoo6s/

相關問題