2010-09-17 37 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

相关问题