2012-01-19 86 views
3

我在我的頁面上有一個表單,讓用戶可以根據需要添加新字段。我使用了jQuery clone()方法來實現這一目標,這裏是我使用的代碼片段:jQuery的克隆()字段沒有通過

// Dynamic forms 
$('.addField').bind('click', function() { 
    var tmp = '#' + $(this).attr('parent') + ' tr td div.clonedInput'; // get the path to elements 
    var x = $(this).attr('parent').split('_'); // get the element's identifier 
    var ident = '#' + x[0]; 

    var num = $(tmp).length; // count elements 
    var newNum = new Number(num + 1); // increase the number of the new element 

    // Create the new element and manipulate its id 
    tmp = x[0] + '_' + newNum; 
    var newElem = $(ident + '_' + num).clone().attr('id', tmp); 

    // Manipulate the new element's children 
    newElem.find('input, select, textarea, label').each(function() { 
     var kid = $(this); 
     var y = ''; 
     var z; 

     y = kid.attr(kid.attr('for') == undefined ? 'name' : 'for').split(x[0] + '_' + num); 
     z = tmp + y[1]; 
     if (kid.attr('for') == undefined) { 
      kid.attr('name', z); 
      kid.attr('id', z); 
      if (kid[0].tagName.toLowerCase() == 'textarea') { 
       kid.html(''); 
      } else { 
       kid.attr('value', ''); 
      } 
     } else { 
      kid.attr('for', z); 
     } 
    }); 

    // Add the new element after the last one 
    $(ident + '_' + num).after(newElem); 

    // Enable the associated "Remove" button 
    $('.remField[parent="' + $(this).attr('parent') + '"]').attr('disabled', ''); 
}); 

我遇到的問題是,動態添加字段不包含在表單提交的結果。我試過這段代碼:

$('form').submit(function() { 
    alert($(this).serialize()); 
}); 

並且字段沒有顯示在結果字符串中。

代碼有什麼問題?

編輯: 原始的HTML

<h1>Add a recipe</h1> 
<form name="add-recipe" action="add-recipe/" method="post" enctype="multipart/form-data"> 
<table class="form"> 
     <input type="hidden" name="formname" value="add-recipe" /> 
     <tr> 
      <td>&nbsp;</td> 
      <td><span style="font-weight: bold;">Basic details</span></td> 
     </tr> 
     <tr> 
      <td><label for="name">Recipe name:</label></td> 
      <td><input type="text" name="rec_name" id="name" maxlength="100" /></td> 
     </tr> 
     <tr> 
      <td><label for="img">Recipe image:</label></td> 
      <td><input type="file" name="rec_img" id="img" /></td> 
     </tr> 
     <tr> 
      <td><label for="cat">Category:</label></td> 
      <td> 
       <select name="rec_cat" id="cat"> 
        <option value="-1"> (Select a category) </option> 
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td><label for="desc">Recipe description:</label></td> 
      <td class="center"><textarea maxlength="1024" name="rec_desc" id="desc" cols="60" rows="10"></textarea></td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><span style="font-weight: bold;">Times and servings</span></td> 
     </tr> 
     <tr> 
      <td><label for="prep">Preparation time:</label></td> 
      <td><input type="text" name="rec_prep" id="prep" maxlength="5" /></td> 
     </tr>  
     <tr> 
      <td><label for="cook">Cooking time:</label></td> 
      <td><input type="text" name="rec_cook" id="cook" maxlength="5" /></td> 
     </tr>  
     <tr> 
      <td><label for="serving_size">Serving size:</label></td> 
      <td><input type="text" name="rec_serving_size" id="serving_size" maxlength="45" /></td> 
     </tr>  
     <tr> 
      <td><label for="servings_per_recipe">Servings per recipe:</label></td> 
      <td><input type="text" name="rec_servings_per_recipe" id="servings_per_recipe" maxlength="45" /></td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><span style="font-weight: bold;">Ingredients</span></td> 
     </tr> 
     <!-- INGREDIENTS  --> 
     <tr> 
      <td>&nbsp;</td> 
      <td> 
       <table class="subform" id="ing_container"> 
        <tr> 
         <td> 
          <div class="clonedInput" id="ing_1"> 
           <div> 
            <label for="ing_1_ing">Ingredient:</label><br /> 
            <select name="ing_1_ing" id="ing_1_ing"> 
             <option value="-1"> (Select an ingredient) </option>     
            </select> 
           </div> 
           <div> 
            <label for="ing_1_amount">Amount:</label><br /> 
            <input type="text" name="ing_1_amount" id="ing_1_amount" maxlength="5" /> 
           </div> 
           <div> 
            <label for="ing_1_det">Details:</label></br /> 
            <textarea maxlength="255" name="ing_1_det" id="ing_1_det" cols="56" rows="8"></textarea> 
           </div> 
           <div> 
            <label for="ing_1_meas">Measurement:</label><br /> 
            <input type="text" name="ing_1_meas" id="ing_1_meas" maxlength="45" /> 
           </div>       
          </div> 
         </td> 
        </tr> 
       </table> 
      </td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td class="center"> 
       <input type="button" class="addField" parent="ing_container" value="Add ingredient" />&nbsp;<input type="button" disabled="disabled" parent="ing_container" class="remField" value="Remove ingredient" /> 
      </td> 
     </tr> 
     <!-- STEPS --> 
     <tr> 
      <td>&nbsp;</td> 
      <td><span style="font-weight: bold;">Steps</span></td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td> 
       <table class="subform" id="step_container"> 
        <tr> 
         <td> 
          <div class="clonedInput" id="step_1"> 
           <div> 
            <label for="step_1_desc">Description:</label></br /> 
            <textarea maxlength="1024" name="step_1_desc" id="step_1_desc" cols="56" rows="8"></textarea> 
           </div>      
          </div> 
         </td> 
        </tr> 
       </table> 
      </td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td class="center"> 
       <input type="button" class="addField" parent="step_container" value="Add step" />&nbsp;<input type="button" disabled="disabled" parent="step_container" class="remField" value="Remove step" /> 
      </td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><span style="font-weight: bold;">Nutrition facts</span></td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td class="center"><input type="submit" value="Proceed" /></td> 
     </tr> 
</table> 
    </form> 
+1

您確定新的表單元素位於'

'標記之間嗎? – Blazemonger

+0

請將此張貼爲答案。我沒有檢查過。 – Pateman

+0

如果你發佈了原始的HTML代碼,這也會有所幫助,特別是因爲你似乎正在使用一些不尋常的'父'屬性的一些不尋常的技巧(可能會破壞事物)。 – Blazemonger

回答

2

你確定新的表單元素所在的<form></form>標籤之間?在Chrome或IE或Firebug for Firefox中使用DOM檢查器來確保。

+0

謝謝你,mblase75!我甚至不會看這個。 – Pateman

+0

我遇到了這個問題,並導致我發現我需要改變我打開表格的位置。謝謝! – Yuschick