我正在考虑如何在mongodb中保存动态表单字段。我已经掌握了使用静态字段保存基本表单的知识。
我想学习的是如何保存动态表单字段(用户可以添加(n)字段/ s)
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<input type="hidden" name="count" value="1" />
<div id="fields">
<div class="controls" id="profs">
<form class="input-append">
<div id="field">
<!--<input autocomplete="off" class="input form-control" id="field1" name="prof1" type="text" placeholder="Type something" data-items="8"/>\-->
<div class="row" id="field1">
<div class="col-xs-12 col-sm-12 col-md-4 form-group">
<select class="form-control">
<option>Select Product</option>
<option>option 2</option>
<option>option 3</option>
<option>option 4</option>
<option>option 5</option>
</select>
</div>
<div class="col-xs-12 col-sm-12 col-md-2 form-group">
<input type="number" name="quantity" class="form-control input" placeholder="Quantity">
</div>
<div class="col-xs-12 col-sm-12 col-md-2 form-group">
<input type="number" name="quantity" class="form-control input" placeholder="Unit Price">
</div>
<div class="col-xs-12 col-sm-12 col-md-2 form-group">
<input type="number" name="quantity" class="form-control input" placeholder="Amount">
</div>
</div>
<button id="b1" class="btn add-more btn-success" type="button"><span class="glyphicon glyphicon-plus"></span>
</button>
</div>
</form>
</div>
</div>
</div>
</div>
//动态表单的脚本
var next = 1;
$(".add-more").click(function(e) {
e.preventDefault();
var addto = "#field" + next;
var addRemove = "#field" + (next);
next = next + 1;
var newIn = '<div class="row" id="field' + next + '"><div class="col-xs-12 col-sm-12 col-md-4 form-group"> <select class="form-control"><option>Select Product</option><option>option 2</option>option>option 3<option>option 5</option></select></div><div class="col-xs-12 col-sm-12 col-md-2 form-group"> <input type="number" name="quantity" class="form-control input" placeholder="Quantity"></div><div class="col-xs-12 col-sm-12 col-md-2 form-group"> <input type="number" name="quantity" class="form-control input" placeholder="Unit Price"></div><div class="col-xs-12 col-sm-12 col-md-2 form-group"> <input type="number" name="quantity" class="form-control input" placeholder="Amount"></div></div>';
var newInput = $(newIn);
var removeBtn = '<button id="remove' + (next - 1) + '" class="btn btn-danger remove-me" >-</button></div><div id="field">';
var removeButton = $(removeBtn);
$(addto).after(newInput);
$(addRemove).after(removeButton);
$("#field" + next).attr('data-source', $(addto).attr('data-source'));
$("#count").val(next);
$('.remove-me').click(function(e) {
e.preventDefault();
var fieldNum = this.id.charAt(this.id.length - 1);
var fieldID = "#field" + fieldNum;
$(this).remove();
$(fieldID).remove();
});
});