jQuery clone() 字段未通过
我的页面上有一个表单,使用户可以根据需要添加新字段。我使用 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>
I have a form on my page which gives the user the possibility to add new fields on demand. I use the jQuery clone()
method to achieve that, here's the snippet I'm using:
// 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', '');
});
The problem I'm having is that the dynamically added fields are not included in the results of form submission. I tried this snippet:
$('form').submit(function() {
alert($(this).serialize());
});
and the fields are not shown in the resulting string.
What is wrong with the code?
EDIT:
Original 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定新的表单元素位于
标记之间吗?使用 Chrome 或 IE 中的 DOM 检查器或 Firefox 中的 Firebug 进行确认。
Are you sure the new form elements reside between the
<form></form>
tags? Use the DOM inspector in Chrome or IE or Firebug for Firefox to make sure.