Javascript 在另一个id jquery中访问id

Javascript 在另一个id jquery中访问id,javascript,jquery,Javascript,Jquery,这就是我要做的。我试图获得一段用div编写的代码,然后将一些div/id修改为不同的名称,这样我就可以用唯一的名称将它们发送到表单中。有什么办法吗? 我现在的想法是首先将变量设置为要复制的div,然后访问其中的另一个div,但这似乎不起作用。有什么想法吗 var i = 0; $("#custom_rates").click(function() { var sublet_dates_seen = $("#sublet_dates_seen").clone()

这就是我要做的。我试图获得一段用div编写的代码,然后将一些div/id修改为不同的名称,这样我就可以用唯一的名称将它们发送到表单中。有什么办法吗? 我现在的想法是首先将变量设置为要复制的div,然后访问其中的另一个div,但这似乎不起作用。有什么想法吗

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;
    }); 

您试图以错误的方式混合DOM和jQuery。必须使用这种方法。另外,代码中间的括号太多了

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;
}); 

最好使用jQuery的.find()方法替换getElementById调用:

另外,我认为.clone()返回一个HTML元素或一组元素,而不是一个jQuery对象,因此您必须执行以下操作:

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

getElementById
既不是jQuery函数,也不是在
文档
之外的任何地方定义的。请参阅:由于ID必须是唯一的,因此在元素中搜索特定ID是没有意义的<代码>$(“#CustomPricePerNightPrice”).attr(…)应该可以。如果有多个元素具有相同的ID,则表示您做错了。