将下拉菜单添加到我的动态表单jQuery中

时间:2014-01-05 17:18:00

标签: javascript jquery html css

我正在创建一个动态表单,用户可以在其中动态添加元素。我能够允许他们添加文本框但是我不知道如何将我的下拉菜单添加到其中。这是我的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下方的位,我必须将其全部粘贴才能使其正常工作。

3 个答案:

答案 0 :(得分:1)

您似乎克隆 div 中的元素。我建议您继续使用 .clone() 方法来执行此操作。

查看非常简化的代码。现在,您可以删除add元素并将其更改为删除。

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

JSFiddle

除非有必要,否则尽量避免使用id

答案 1 :(得分:0)

如果你想在每个“添加”点击上添加一行,那么我会尝试这样的事情:

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

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

有关工作示例,请参阅this jsFiddle

编辑: 既然你需要控制你的id,我就会搞一些应该有用的东西,

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);
        }
    }

});

答案 2 :(得分: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);