脚本成功添加文本字段但低于上一个而不是上面

时间:2015-07-23 06:23:49

标签: javascript

我在将数据添加到上一个文件后使用此脚本添加文本字段,但下一个文本字段在前一个底部增加。我想要上一个空白文本字段。请帮忙。

var next1 = 1;
                $(".add-more1").click(function(e){
                    e.preventDefault();
                    var addto1 = "#fld" + next1;
                    var addRemove1 = "#fld" + (next1);
                    next1 = next1 + 1;
                    var newIn1 = '<input list="type" id="type' + next1 +'" name="type' + next1 + '"   placeholder="Select type"> ';
                    var newIn2 = '<input class="input"  list="name" id="fld' + next1 + '" name="fld' + next1 + '" placeholder="Add member"/>';
                    var newInput1 = $(newIn1);
                    var newInput2 = $(newIn2);
                    var removeBtn1 = '<button id="remove' + (next1 - 1) + '" class="btn btn-danger remove-me" >-</button></div><div id="fld">';
                    var removeBtn1 = '<button id="remove' + (next1 - 1) + '" class="btn btn-danger remove-me" >-</button></div><div id="type">';
                    var removeButton1 = $(removeBtn1);


                    $(addto1).after(newInput2);
                    $(addto1).after(newInput1);
                    $(addRemove1).after(removeButton1);
                    $("#type" + next1).attr('data-source',$(addto1).attr('data-source'));
                    $("#fld" + next1).attr('data-source',$(addto1).attr('data-source'));
                   // $("#count").val(next1);  

                        $('.remove-me').click(function(e){
                            e.preventDefault();
                            var fieldNum1 = this.id.charAt(this.id.length-1);
                            var fieldID1 = "#fld" + fieldNum1;
                            $(this).remove();
                            $(fieldID1).remove();
                            var fieldNum2 = this.id.charAt(this.id.length-1);
                            var fieldID2 = "#type" + fieldNum2;
                            $(this).remove();
                            $(fieldID2).remove();
                        });
                });

1 个答案:

答案 0 :(得分:0)

$(addto1).before(newInput2);
$(addto1).before(newInput1);
$(addRemove1).before(removeButton1);