2011-11-11 12 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オブジェクトを返します。 –

関連する問題