2011-11-11 99 views
0

這是我正在嘗試做的。我試圖獲得用div編寫的代碼塊,然後將一些div/id修改爲不同的名稱,因此我可以將它們發送到具有唯一名稱的表單。任何想法如何做到這一點? 我現在的想法是首先將變量設置爲我正在複製的div,然後訪問其中的其他div,但這似乎不起作用。有任何想法嗎?訪問另一個ID內的ID jquery

var i = 0; 
    $("#custom_rates").click(function() 
    { 
     var sublet_dates_seen = $("#sublet_dates_seen").clone(); 
     // all id's below are within #sublet_dates_seen 
     sublet_dates_seen.getElementById('#CustomPricePerNightPrice').attr('name','data[CustomPricePerNight][price][' + i ']'); 
     sublet_dates_seen.getElementById(('#CustomPricePerNightStartDateMonth').attr('name','data[CustomPricePerNight][start_date][month][' + i ']'); 
     sublet_dates_seen.getElementById(('#CustomPricePerNightStartDateDay').attr('name','data[CustomPricePerNight][start_date][day][' + i ']'); 
     sublet_dates_seen.getElementById(('#CustomPricePerNightStartDateYear').attr('name','data[CustomPricePerNight][start_date][year][' + i ']'); 
     sublet_dates_seen.getElementById(('#CustomPricePerNightEndDateMonth').attr('name','data[CustomPricePerNight][end_date][month][' + i ']'); 
     sublet_dates_seen.getElementById(('#CustomPricePerNightEndDateDay').attr('name','data[CustomPricePerNight][end_date][day][' + i ']'); 
     sublet_dates_seen.getElementById(('#CustomPricePerNightEndDateYear').attr('name','data[CustomPricePerNight][end_date][year][' + i ']'); 
     sublet_dates_seen.getElementById(('#CustomPricePerNightMinimumNights').attr('name','data[CustomPricePerNight][minimum_nights][' + i ']'); 
     sublet_dates_seen.getElementById(('#CustomPricePerNightMaximumNights').attr('name','data[CustomPricePerNight][maximum_nights][' + i ']');   
     sublet_dates_seen.appendTo(".avi_specialrates"); 
     i = i + 1; 
    return false; 
    }); 
+2

'g etElementById'既不是jQuery函數,也不是在'document'之外的任何地方定義的。請參閱:https://developer.mozilla.org/en/DOM/document.getElementById – Zirak

+1

由於ID必須是唯一的,因此在元素內搜索某個ID是沒有意義的。 '$('#CustomPricePerNightPrice')。attr(...)'應該沒問題。如果你有多個具有相同ID的元素,那麼你做錯了什麼。 –

回答

2

您試圖以錯誤的方式混合使用DOM和jQuery。必須使用.find()方法。另外,你的代碼中間有太多的括號。

sublet_dates_seen.getElementById('#CustomPricePerNightPrice'); //WRONG 
sublet_dates_seen.find('#customPricePerNight'); //Correct 

正確的代碼:

var i = 0; 
$("#custom_rates").click(function() 
{ 
    var sublet_dates_seen = $("#sublet_dates_seen").clone(); 
    // all id's below are within #sublet_dates_seen 
    sublet_dates_seen.find('#CustomPricePerNightPrice').attr('name','data[CustomPricePerNight][price][' + i ']'); 
    sublet_dates_seen.find('#CustomPricePerNightStartDateMonth').attr('name','data[CustomPricePerNight][start_date][month][' + i ']'); 
    sublet_dates_seen.find('#CustomPricePerNightStartDateDay').attr('name','data[CustomPricePerNight][start_date][day][' + i ']'); 
    sublet_dates_seen.find('#CustomPricePerNightStartDateYear').attr('name','data[CustomPricePerNight][start_date][year][' + i ']'); 
    sublet_dates_seen.find('#CustomPricePerNightEndDateMonth').attr('name','data[CustomPricePerNight][end_date][month][' + i ']'); 
    sublet_dates_seen.find('#CustomPricePerNightEndDateDay').attr('name','data[CustomPricePerNight][end_date][day][' + i ']'); 
    sublet_dates_seen.find('#CustomPricePerNightEndDateYear').attr('name','data[CustomPricePerNight][end_date][year][' + i ']'); 
    sublet_dates_seen.find('#CustomPricePerNightMinimumNights').attr('name','data[CustomPricePerNight][minimum_nights][' + i ']'); 
    sublet_dates_seen.find('#CustomPricePerNightMaximumNights').attr('name','data[CustomPricePerNight][maximum_nights][' + i ']');   
    sublet_dates_seen.appendTo(".avi_specialrates"); 
    i = i + 1; 
    return false; 
}); 
0

最好的辦法是更換您的getElementById與jQuery的.find()的調用方法:http://api.jquery.com/find/

而且,我相信.clone()返回HTML元素或一組元素,而不是jQuery對象,所以你必須這樣做:

$(sublet_dates_seen).find('#CustomPricePerNightPrice').attr('name','data[CustomPricePerNight][price][' + i ']'); 
+2

['clone()'](http://api.jquery.com/clone/)返回一個jQuery對象。 –