2014-01-05 135 views
0

我正在创建一个动态表单,用户可以在其中动态添加元素。我能够让他们添加文本框,但我不知道如何添加我的下拉菜单到它。这里是我的jQuery代码将下拉菜单添加到我的动态表单中jQuery

var addDiv2 = $('#addIngredient'); 
    var i = $('#addIngredient p').size() + 1; 
    $('#addNewIngredient').on('click', function() { 
     $('<p> <input id="ingredient_' + i + '" name="ingredient[]" type=text" 
      value="" placeholder="Ingredient" /> 
      <input id="quantity_' + i + '" 
      name="quantity[]" type=text" value="" placeholder="Quantity" /> 
      <input id=alternative_' + i + '" name="alternative[]" type=text" 
      value"" placeholder="Alternative Ingredient" /> 
      <a href=#" class="remNew2"> Remove </a> </p> ').appendTo(addDiv2); 

,这里是我的html

<div id="addIngredient"> 
<p> 
<input type="text" id="ingredient_1" name="ingredient[]" value="" 
    placeholder="Ingredient"/> 
<input type="text" id="quantity_1" name="quantity[]" value="" 
placeholder="Quantity"/> 
<input type="text" id="alternative_1" name="alternative[]" value="" 
placeholder="Alternative Ingredient"/> 
<select id="selectQuantity_1" name="quantityType[]"> 
<option value="grams">Grams</option> 
<option value="ounces">Ounces</option> 
<option value="Pounds">Pounds</option> 
</select> 
<a href="#" id="addNewIngredient">Add</a> 

我都试过,但不能做出来,帮助将不胜感激!

这里是的jsfiddle http://jsfiddle.net/3yFFr/

忽略了jQuery我粘贴了以下一点,我不得不给它的所有粘贴为它工作。

回答

0

如果您要添加的每个“添加”点击,那么我会尝试这样一行:

var addDiv2 = $('#addIngredient'); 
var formRow = addDiv2.find('p'); 

$('#addNewIngredient').on('click', function() { 
    formRow.clone(true, true).appendTo(addDiv2); 
}); 

的工作示例见this jsFiddle

编辑: 既然你需要保持你的IDS在办理入住手续,我造了一个小东西,应该工作,

jsFiddle Included

var addDiv2 = $('#addIngredient'); 

$('#addNewIngredient').on('click', function() { 

    // Clone the last input row, keeping the event handlers 
    var clonedRow = $('#addIngredient p').last().clone(true, true); 

    // We're going to examine all select and input elements 
    var all = clonedRow.find('select, input'); 

    // A regular expression we can use to test if the id has numbers at the end 
    var numericPostfix = /\d+$/;  

    for (var i = 0; i < all.length; i++) { 
     var curr = $(all[i]); 

     // If current element has an id attribute 
     if (curr.attr('id') != undefined) { 
      var id = curr.attr('id'); 

      // Rips off any trailing digits in element's id 
      var idNum = numericPostfix.exec(id); 

      // Exec gives an array of matches, if it's not null, choose the first match 
      if (idNum) { 
       idNum = idNum[0]; 
      } 

      if (idNum) {    
       // Chop off last character 
       id = id.slice(0, (-1 * idNum.length)); 
       // Increment and add the id number to the nextId 
       var nextId = id + (Number(idNum) + 1); 
       // Set the id attribute to nextId 
       curr.attr('id', nextId); 
      } 

      // Append to the DOM 
      clonedRow.appendTo(addDiv2); 
     } 
    } 

}); 
+0

我需要这样做,因为我已经完成了上面的操作,所以每个成分的名称都是这样的,因此成分_1成分_2,因为我用PHP处理它后 –

0

在你的代码,你没有将选择菜单添加到您使用jQuery附加的内容。看:

$('<p> <input id="ingredient_' + i + '" name="ingredient[]" type=text" value="" 
placeholder="Ingredient" /> 
<input id="quantity_' + i + '" name="quantity[]" type=text" value="" 
    placeholder="Quantity" /> 
<input id=alternative_' + i + '" name="alternative[]" 
    type=text" value"" placeholder="Alternative Ingredient" /> 
<a href=#" class="remNew2"> Remove </a> </p> ').appendTo(addDiv2); 
+0

我知道,这就是为什么我问如何将它添加到jQuery ,我试图和只是得到错误 –

+0

@NickPocock在'appendTo',尝试添加一个选择器(也许像''nth-child(2)''而不是一个变量。 – hitecherik

1

看来你克隆DIV中的元素。我建议你继续使用.clone()方法来做到这一点。

看到相当简化的代码。现在,您可以在这里删除添加元素并将其更改为删除。

$('#addIngredient').clone() 
      .prop('id', 'addIngredient1').appendTo('ingredDiv'); 

JSFiddle

而且还尽量避免使用id,除非它是必要的。

+0

我正在处理它们与PHP,所以我有每个成分都有一个数字,这是可能的.clone()? –

+0

@NickPocock我认为这是可能的,但需要大量的工作,同时我不明白你为什么不能够添加选择为其他。看到我已经增加与增长http://jsfiddle.net/3yFFr/2/ – Praveen

+1

多数民众赞成在多数民众赞成在完美的时候,当我试图保持显示选项作为明文而不是HTML代码并不能正常工作谢谢 –

相关问题