2010-09-17 38 views
0

我想添加_1,_2等到我的輸入元素的動態形式的結尾。用戶可以添加儘可能多的,因爲他們希望我寫的這個方法,但它似乎拋出錯誤說法「DNAME未定義」附加數值與jQuery輸入名稱屬性

$j("#addBranch").click(
        function() 
         { 
          //see withDataAndEvents, copies events with bool in clone method 
          $j('.branchOffice').first().clone(true).appendTo('#branchOffice'); 
          $j('.branchOffice').last().children().attr('name',function() 
          { 
           var dName = $j(this.name).attr('name'); 
            return dName.substring(0, dName.length-1) + ($j('.branchOffice').size() + 1) 
           });  
         } 
        ); 

下面是HTML

<div class="leftPane"> 
     <label for="company_name">Company Name</label> 
     <input class="req" type="text" name="company_name" id="company_name" /> 
     <label for="branch_office">Home Office</label> 
     <input class="req" type="text" name="branch_office_1" id="branch_office_1" /> 
     <label for="branch_office_add1_1">Address</label> 
     <input class="req" type="text" name="branch_office_add1_1" id="branch_office_add1_1" /> 
     <label for="branch_office_add2_1">Address 2</label> 
     <input type="text" name="branch_office_add2_1" id="branch_office_add2_1" /> 
     <label for="branch_office_city_1">City</label> 
     <input class="req" type="text" name="branch_office_city_1" id="branch_office_city_1" /> 
     <label for="branch_office_state_1">State</label> 
     <select id="branch_office_state_1" name="branch_office_state_1"> 
      <?php echo $formHelper->stateList; ?> 
     </select> 
     <label class="naked" for="branch_office_zip_1">Zip</label> 
     <input class="req zip" type="text" name="branch_office_zip_1" id="branch_office_zip_1" /> 
    </div> 
    <div class="rightPane" id="branchOffice"> 
     <div class="branchOffice"> 
      <label for="branch_office_2">Branch Office</label> 
      <input class="req" type="text" name="branch_office_2" id="branch_office_2" /> 
      <label for="branch_office_add1_2">Address</label> 
      <input class="req" type="text" name="branch_office_add1_2" id="branch_office_add1_2" /> 
      <label for="branch_office_add2_2">Address 2</label> 
      <input type="text" name="branch_office_add2_2" id="branch_office_add2_2" /> 
      <label for="branch_office_city_2">City</label> 
      <input class="req" type="text" name="branch_office_city_2" id="branch_office_city_2" /> 
      <label for="branch_office_state_2">State</label> 
      <select id="branch_office_state_2" name="branch_office_state_2"> 
       <?php echo $formHelper->stateList; ?> 
      </select> 
      <label for="branch_office_zip_2">Zip</label> 
      <input class="req zip" type="text" name="branch_office_zip_2" id="branch_office_zip_2" /> 
     </div> 
    </div> 
    <div class="spacer"></div> 
    <small class="red">If more than one branch office, please</small><a class="addMore" title="Add Branch" id="addBranch">Add More</a> 
+0

什麼是您的HTML是什麼樣子?我放棄了它,但很難確定不知道HTML結構。 – 2010-09-17 05:01:00

回答

0
var dName = $j(this).attr('name'); 

,或者現在我再看一次,$j(this)反映到.branhOffice而不是#addBranch

也許設置一個val;

var addBranch = $(this); 

那麼你的另一個選擇則

var dName = $(addBranch).attr('name'); 
+0

我應該更徹底地解釋一下,#addBranch是一個按鈕,.branchOffice是我想複製的office元素的div類,#branchOffice是容器(我應該將它命名爲「#branchesContainer 「 – 2010-09-17 14:34:34

相關問題