访问另一个id jquery中的id

时间:2011-11-11 18:28:18

标签: javascript jquery

这就是我想要做的。我正在尝试使用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;
    }); 

2 个答案:

答案 0 :(得分: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;
}); 

答案 1 :(得分:0)

最好的办法是用jQuery的.find()方法替换你的getElementById调用:http://api.jquery.com/find/

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

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