2011-08-25 157 views
1

我有以下代碼。如果我沒有table tr td標籤,我可以訪問輸入框和廣播框。通過只有輸入和單選按鈕,我使用eq(x)。 x是數字。使用jQuery訪問兒童div標記

var newElem = $('#input' + num).clone().attr('id', 'input' + newNum); 
newElem.children(:eq(0)').attr('id', 'frmBuyerComments_RsnCode' + newNum).attr('name', 'frmBuyerComments_RsnCode' + newNum);  
newElem.children(':eq(1)').attr('id', 'frmBuyerComments_money' + newNum).attr('name', 'frmBuyerComments_RadioGroup' + newNum); 
newElem.children(':eq(2)').attr('id', 'frmBuyerComments_percentage' + newNum).attr('name', 'frmBuyerComments_RadioGroup' + newNum); 

但我不知道添加的表標籤時如何訪問它們。這裏是html代碼。 我怎樣newElem.children()播放訪問如上使用newElem.children所示()

<div id="inputForm"> 

    <div id="input1" style="margin-bottom:4px;" class="clonedInput"> 
     <table cellspacing="0" cellpadding="2" width="100%" style="margin:10px 0px 0px 0px;" id="TableReasonCode"> 
      <tr> 
       <td width="25%">Reason Code</td> 
       <td class="_CommentsData"><select id="frmBuyerComments_RsnCode1" runat="server"></select></td> 
      </tr> 
      <tr> 
       <td width="25%">Symbol</td> 
       <td class="_CommentsData"><input type="radio" id="frmBuyerComments_money1" name="frmBuyerComments_RadioGroup1" value="$" checked="checked" />$ <input type="radio" id="frmBuyerComments_percentage1" name="frmBuyerComments_RadioGroup1" value="%" /> %</td> 
      </tr> 
     </table> 
     <%--Reason Code : <select id="frmBuyerComments_RsnCode1" runat="server"></select> 
     Denominator : <input type="radio" id="frmBuyerComments_money1" name="frmBuyerComments_RadioGroup1" value="$" checked="checked" />$ <input type="radio" id="frmBuyerComments_percentage1" name="frmBuyerComments_RadioGroup1" value="%" /> %--%> 
    </div>  

    <div> 
     <input type="button" id="btnAdd" class="_Buttons" onclick="AddExtraField()" value="More Reason Code" /> 
     <input type="button" id="btnDel" class="_Buttons" onclick="RemoveExtraField()" value="Remove Reason Code" /> 
    </div> 

    <table cellspacing="0" cellpadding="2" width="100%" style="margin:10px 0px 0px 0px;" id="TableOtherInputs"> 
     <tr> 
      <td width="25%">Comments</td> 
      <td class="_CommentsData"><textarea rows="4" cols="24" id="frmBuyerComments_ByerComments" name="frmBuyerComments_ByerComments"></textarea></td> 
     </tr> 
     <tr> 
      <td class="_CommentsLabel"></td> 
      <td class="_CommentsData"></td> 
     </tr> 
    </table> 

</div> 

回答

2

如果你只是想在你的新項目獲得的輸入保持那麼:

$('#input'+newNum).find('select') 
$('#input'+newNum).find('input[type="radio"]') 

但是,我猜這不適合你,因爲你實際上正在做一些$(document).ready調用中的東西,而這些東西還不存在。是這樣嗎?

+0

我只是想通過使用div id =「input1」來克隆無線電。然後,當我有另一個div input2時,我想更改的id值,以便它不會與上一個id相同。 – user696383

+0

更好的問題是「爲什麼他們首先有ID」。如果某物是獨特的蝴蝶,則僅使用ID。如果你所持有的是一種「類型的東西」,那就給它上一堂課。根據它的位置或其他屬性來定位它。 ID有一個目的,併爲舞臺上的每個元素提供標籤不是它。 – Sinetheta

+0

我使用$('#input'+ newNum).find('select:eq(0)')。與無線電類型相同,通過添加:eq(x)現在可以工作。 – user696383

0

這是因爲你明確告訴jQuery來挑的第一個孩子。相反,你應該告訴它來獲得的第一個孩子是一個輸入框(或選擇)

newElem.children('select:eq(0)'); //first select box 
newElem.children('input:eq(0)'); //first input box 
+0

.find()對無線電工作正確的,因爲每Sinetheta答案。謝謝。 – user696383

0

而不是使用.children我會使用.find和一些表單jQuery擴展選擇器。

http://api.jquery.com/category/selectors/

因此,像 newElem.find(「....由class和id或者類似的東西插入選擇:單選」)從您發佈給小樣本的硬

類更詳細的答案。

0

你可以試試這個代碼,你的函數:

var num = 1; 
function AddExtraField(){ 
    var old="div#input"+num; 
    var newNum = num+1; 
    var newElem = $(old).clone(); 
    $(newElem).attr('id', 'input' + newNum); 
    $(newElem).appendTo($(old)); 
    $("#frmBuyerComments_money"+num, newElem) 
     .attr("id","#frmBuyerComments_money"+newNum) 
     .attr('name', 'frmBuyerComments_RadioGroup' + newNum); 

    $("#frmBuyerComments_percentage"+num, newElem) 
     .attr("id","#frmBuyerComments_percentage"+newNum) 
     .attr('name', 'frmBuyerComments_RadioGroup' + newNum); 
    $(newElem).effect('highlight',{},1000,function(){}); 
    ++num; 
}