这是我正在尝试做的。我试图获得用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的元素,那么你做错了什么。 –