2011-11-11 4 views
0

Voici ce que j'essaie de faire. J'essaie d'obtenir un bloc de code écrit en div puis de modifier une partie de la div/id en différents noms et ainsi je peux les envoyer au formulaire avec des noms uniques. auriez vous des idées pour faire ça? Mon idée en ce moment est de mettre d'abord la variable à la div que je copie puis d'accéder à l'autre div à l'intérieur, mais cela ne semble pas fonctionner. Des idées?accéder à un identifiant dans un autre identifiant 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' n'est ni une fonction jQuery ni définie ailleurs sauf sur 'document'. Voir: https://developer.mozilla.org/en/DOM/document.getElementById – Zirak

+1

Comme les ID doivent être uniques, il est inutile de rechercher un ID spécifique dans un élément. '$ ('# CustomPricePerNightPrice'). Attr (...)' devrait être très bien. Si vous avez plusieurs éléments avec le même ID, vous faites quelque chose de mal. –

Répondre

2

Vous essayez de mélanger DOM et jQuery de manière incorrecte. La méthode .find() doit être utilisée. En outre, vous avez trop de parenthèses au milieu de votre code.

sublet_dates_seen.getElementById('#CustomPricePerNightPrice'); //WRONG 
sublet_dates_seen.find('#customPricePerNight'); //Correct 

Le code 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

Votre meilleur pari est de remplacer votre getElementById appelle avec .Find jQuery() Méthode: http://api.jquery.com/find/

Aussi, je crois .clone() retourne une Élément HTML ou ensemble d'éléments, pas un objet jQuery, vous devez donc faire quelque chose comme ceci:

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

['clone()'] (http://api.jquery.com/clone/) renvoie un objet jQuery. –

Questions connexes