2012-08-06 56 views
0

我有一個表格中的表格可以選擇複製某一行(從而允許用戶根據需要添加更多的字段)。克隆工作正常,事件數據和處理程序正常工作。但是,當提交表單時,克隆的表單字段數據不會發布。下面是我的代碼克隆工作正常的領域,並解決任何重複ID的問題。任何建議/幫助爲什麼克隆領域不提交將絕對讚賞。

$('#btnAddNewField').click(function() { 
    var currLength = $('.cloneInput').length; 
    var newID = new Number(currLength + 1); 

    var clonedField = $('#field_id' + currLength).clone(true); 
    clonedField[0].setAttribute('id', 'field_id' + newID);  
    clonedField.find(':text').each(function() {        
     this.setAttribute('id', this.getAttribute('id') + newID);    
     this.setAttribute('name', this.getAttribute('name') + newID);  
    }); 
    $('#field_id' + currLength).after(clonedField); 

HTML:

<tr id="field_id_1"> 
    <td> 
    <table>    
     <tr> 
      <td>Pick option</td> 
      <td> 
       <select name="choice_1" id="choice_1" parent="true"> 
        <option value="null" selected="selected">Select</option> 
        <option value="1">option 1</option> 
        <option value="2">option 2</option> 
        <option value="3">option 3</option>    
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td>Pick next option</td> 
      <td> 
       <select name="next_choice_1" id="next_choice_1"> 
        <option value="null" selected="selected">Select next</option>     
       </select> 
      </td> 
     </tr>    
     <tr> 
      <td>Image:</td> 
      <td><input type="file" name="image_1" id="image_1"/></td> 
     </tr>   
    </table> 
    </td>        
</tr> 
<tr> 
    <td></td> 
    <td> 
     <input type="button" value="Add New" id="btnAddNewField" /> 
     <input type="button" value="Remove" id="btnDelField" /> 
    </td>   
</tr> 
+0

是否缺少來自'.find(':text)'的拼寫錯誤?或者是你的實際代碼中的錯誤?提示:*看*在語法高亮。 – 2012-08-06 22:23:04

+0

是的,這是一個錯字。 – Brickgame 2012-08-08 04:12:23

+0

你在這個jQuery代碼執行後看到了輸出HTML嗎?克隆的fielsd應該具有唯一名稱,或者是帶有'[]'的相同名稱,即'foo []'。然後所有的值將被保存在'foo [0]','foo [1]'...'foo [index]'這種方法也可以避免使用計數器。至於唯一的ID,你可以使用'class'而不是'ID' .. – DavChana 2012-08-08 04:24:22

回答

1

我沒有看到任何地方,你正在改變克隆表單字段的name屬性。您將需要更改此設置,以便不會將發佈的數據僅由其他字段覆蓋。

+0

我添加了更改到克隆字段的名稱屬性,但仍然無效。查看更新的代碼示例。 – Brickgame 2012-08-08 04:18:59

+0

看到一些示例HTML源代碼會很有幫助。 – 2012-08-08 20:52:45

0

我想你是錯誤的郵政工作。您應該實際爲克隆的字段創建一個新名稱,因爲這是用於發佈變量的名稱。創建一個新的id只需要生成有效的html,但與實際發佈無關。