這是我正在嘗試做的。我試圖獲得用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;
});
'g etElementById'既不是jQuery函數,也不是在'document'之外的任何地方定義的。請參閱:https://developer.mozilla.org/en/DOM/document.getElementById – Zirak
由於ID必須是唯一的,因此在元素內搜索某個ID是沒有意義的。 '$('#CustomPricePerNightPrice')。attr(...)'應該沒問題。如果你有多個具有相同ID的元素,那麼你做錯了什麼。 –