当我点击添加行按钮时,如何在html中动态地向行添加行?

时间:2018-03-27 08:08:49

标签: javascript jquery html vue.js vue-component

<div class="row">
    <div class="col-md-4">
        <div class="form-group label-floating">
            <label class="control-label">Act</label>
            <input type="text" class="form-control" v-model="act" >
        </div>
    </div>
    <div class="col-md-4">
        <div class="form-group label-floating">
            <label class="control-label">Section </label>
            <input type="text" class="form-control" v-model="section">
        </div>
    </div>
    <button>Add row</button>
</div>

所以,当我点击添加按钮时,我需要继续添加上面的行。当我点击添加行按钮时,如何添加此行。

我需要传递值为BOOKED UNDER:

[{
    act :,
    section:,
}]

如果我有更多行,我需要以逗号分隔的方式传递值。我在js中很弱,这是我第一个遇到这种问题的项目。我怎样才能以这种方式添加值。

我的vue js代码是

addForm = new Vue({
    el: "#addForm",
    data: {
        bookedUnder:[],
        act: '',
        section:'',
    },
    methods: {
        handleSubmit: function(e) {
            var vm = this;
            data['otherNatureofOffense'] = //in the abve way
            $.ajax({
                url: 'http://localhost:3000/record/add/f/',
                data: data,
                type: "POST",
                dataType: 'json',
                success: function(e) {
                    if (e.status) {
                        vm.response = e;
                        alert("success")
                    } else {
                        vm.response = e;
                        console.log(vm.response);
                        alert(" Failed") 
                    }
                }
            });
            return false;
        }, 
    },
});

请帮我解决问题

4 个答案:

答案 0 :(得分:2)

请试试,

document.getElementById("clickMe").onclick = function () { 
        //first div
        var newDivCol = document.createElement("div");
        newDivCol.setAttribute("class","col-md-4");
        //second div
        var newDivForm = document.createElement("div");
        newDivForm.setAttribute("class","form-group label-floating");
        newDivCol.appendChild(newDivForm);

        //label
        var newlabel = document.createElement("label");
        newlabel.setAttribute("class","control-label");
        newlabel.innerHTML = "Here goes the text";
        newDivForm.appendChild(newlabel);

        //input
        var newInput = document.createElement("input");
        newInput.setAttribute("type","text");
        newInput.setAttribute("class","form-control");
        newInput.setAttribute("v-model","act");
        newDivForm.appendChild(newInput);

        var element = document.getElementById("addRowsHere");
        element.appendChild(newDivCol);
};
<div class="row" id="addRowsHere">
    <div class="col-md-4">
        <div class="form-group label-floating">
            <label class="control-label">Act</label>
            <input type="text" class="form-control" v-model="act" >
        </div>
    </div>
    <div class="col-md-4">
        <div class="form-group label-floating">
            <label class="control-label">Section </label>
            <input type="text" class="form-control" v-model="section">
        </div>
    </div>
</div>
<button id="clickMe">Add row</button>

https://jsfiddle.net/kkyunLzg/2/#

答案 1 :(得分:2)

如果您使用纯JavaScript,这可能会有所帮助

&#13;
&#13;
var count=1;
$("#btn").click(function(){
  
  $("#container").append(addNewRow(count));
  count++;
});

function addNewRow(count){
  var newrow='<div class="row">'+
    '<div class="col-md-4">'+
        '<div class="form-group label-floating">'+
            '<label class="control-label">Act '+count+'</label>'+
            '<input type="text" class="form-control" v-model="act" >'+
        '</div>'+
    '</div>'+
    '<div class="col-md-4">'+
        '<div class="form-group label-floating">'+
            '<label class="control-label">Section '+count+'</label>'+
            '<input type="text" class="form-control" v-model="section">'+
        '</div>'+
    '</div>'+    
'</div>';
  return newrow;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div id="container">
<div class="row">
    <div class="col-md-4">
        <div class="form-group label-floating">
            <label class="control-label">Act</label>
            <input type="text" class="form-control" v-model="act" >
        </div>
    </div>
    <div class="col-md-4">
        <div class="form-group label-floating">
            <label class="control-label">Section </label>
            <input type="text" class="form-control" v-model="section">
        </div>
    </div>
    
</div>
</div>
<button id="btn">Add row</button>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您需要首先对字段进行v-for然后发布模型:

<div class="row" v-for="(book, index) in bookedUnder" :key="index">
    <div class="col-md-4">
        <div class="form-group label-floating">
            <label class="control-label">Act {{index}}</label>
            <input type="text" class="form-control" v-model="book.act" >
        </div>
    </div>
    <div class="col-md-4">
        <div class="form-group label-floating">
            <label class="control-label">Section {{index}}</label>
            <input type="text" class="form-control" v-model="book.section">
        </div>
    </div>

</div>
<button @click="addNewRow">Add row</button>



addForm = new Vue({
    el: "#addForm",
    data: {
        bookedUnder:[
          {
             act: null,
             section: null,
          },
        ],
    },
    methods: {
        addNewRow: function() {
          this.bookedUnder.push({ act: null, section: null, });
        },
        handleSubmit: function(e) {
            var vm = this;
            $.ajax({
                url: 'http://localhost:3000/record/add/f/',
                data: vm.bookedUnder,
                type: "POST",
                dataType: 'json',
                success: function(e) {
                    if (e.status) {
                        vm.response = e;
                        alert("success")
                    } else {
                        vm.response = e;
                        console.log(vm.response);
                        alert(" Failed") 
                    }
                }
            });
            return false;
        }, 
    },
});

答案 3 :(得分:0)

根据您要添加的行的类型,有不同的解决方案。

如果您只想添加&#39; Section&#39; -rows,您可以在Vue中使用v-for并将对象推送到连接到v-for的数组:

模板:

<div class="col-md-4" v-for="(row, index) in rows" :key="index">
  <div class="form-group label-floating">
    <label class="control-label">Section </label>
    <input type="text" class="form-control" v-model="row.section">
  </div>
</div>
<button @click="addRow">
  Add Row
</button>

JS:

 data: {
   rows: [],
   defaultRow: {
     section: 'new-row'
   }
 },
 methods: {
   addRow() {
     // Clone the default row object
     this.rows.push(Object.assign({}, this.defaultRow))
   }
 }

如果你想添加不同类型的行,你必须创建vue组件并添加它们,或者使用不同的defaultRows获得创意(比如为不同的输入添加不同的类型)。