我的页面上有一个表单,用户可以根据需要添加新字段。我使用jQuery clone()
方法来实现这一点,这是我使用的代码片段:
// 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> </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> </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> </td>
<td><span style="font-weight: bold;">Ingredients</span></td>
</tr>
<!-- INGREDIENTS -->
<tr>
<td> </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> </td>
<td class="center">
<input type="button" class="addField" parent="ing_container" value="Add ingredient" /> <input type="button" disabled="disabled" parent="ing_container" class="remField" value="Remove ingredient" />
</td>
</tr>
<!-- STEPS -->
<tr>
<td> </td>
<td><span style="font-weight: bold;">Steps</span></td>
</tr>
<tr>
<td> </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> </td>
<td class="center">
<input type="button" class="addField" parent="step_container" value="Add step" /> <input type="button" disabled="disabled" parent="step_container" class="remField" value="Remove step" />
</td>
</tr>
<tr>
<td> </td>
<td><span style="font-weight: bold;">Nutrition facts</span></td>
</tr>
<tr>
<td> </td>
<td class="center"><input type="submit" value="Proceed" /></td>
</tr>
</table>
</form>
答案 0 :(得分:2)
您确定新的表单元素位于<form></form>
标记之间吗?使用Chrome或IE或Firebug for Firefox中的DOM检查器来确保。