我已经做了半天多了,我无法弄清楚我错过了什么。我尝试了许多不同的迭代以及我能找到的所有其他解决方案。这应该是不费吹灰之力的。我正在使用 JQueryMobile 1.2 它的依赖项这是 javascript(注意var counter = 2;
在代码上方声明:
$("#btn_addIngredient").click(function () {
if (counter > 10) {
alert("Please limit to 10 ingredients");
return false;
}
var newIngredientTextBox = $(document.createElement('div')).attr("id", 'div_ingredient' + counter);
newIngredientTextBox.after().html('<input type="text" name="ingredient' + counter +
'" id="ingredient' + counter + '" value="#' + counter + '" >');
newIngredientTextBox.appendTo("#allIngredientsDiv");
counter++;
}); //~ $("#btn_addIngredient")
和html:
<div id="IngredientsTextGroup" data-role="fieldcontain">
<fieldset id="IngredientsFieldset" data-role="controlgroup" data-mini="true">
<div id="allIngredientsDiv">
Ingredients
<div id="div_ingredient1">
<input id="ingredient1" value="#1" type="text">
</div>
<!-- *** Need to add <div_ingredient2, 3 ...> -->
</div>
<a id="btn_addIngredient" href="#" data-role="button" data-inline="true" data-icon="plus"
data-iconpos="left">
Add Ingredient
</a>
<div>