2011-11-11 98 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对象。 –