我在我的 laravel 5.4 应用程序的视图文件中创建了一个表单,我可以在其中使用 jqueryclone()函数添加更多输入字段。在这种形式中,我还使用 jquery select2 插件。这是表格:
<form name="tempform" action="{{route('temppris.store')}}" method="POST">
{{csrf_field()}}
<table class="table table-hover small-text" id="tb">
<tr class="tr-header">
<th >Sl.No.</th>
<th class="col-lg-2" >Type</th>
<th class="col-lg-3">Medicine Name</th>
<th colspan="2" class="col-lg-2">Dosage</th>
<th colspan="2" class="col-lg-2">Duration</th>
<th>Total Qty</th>
<th class="col-lg-1"> Remarks</th>
<th><a href="javascript:void(0);" style="font-size:18px;" id="addMore" title="Add More Person"><span class="glyphicon glyphicon-plus"></span></a></th>
<tr>
<td><input type="text" name="slno[]" style="width:100%;" readonly></td>
<td ><select name="meditype[]" class="meditype" style="width:100%;"></select> </td> </td>
<td class="col-lg-4"><select name="medicine[]" class="medicine" style="width:100%;"></select></td>
<td><input type="number" value="1" class="dos input-sm" style="width:40px;" min="1" name="dos[]" step="0.1"></td>
<td class="col-lg-2"><select class="dosage" style="width:100%;" name="dosage[]" ></select></td>
<td><input type="number" value="1" class="NoOfDuration input-sm" style="width:40px;" min="1" name="NoOfDuration[]" min="1"></td>
<td class="col-lg-2"><select class="duration" name="duration[]" style="width:100%;"></select></td>
<td><input type="number" class=" total" min="1" name="total[]" style="width:50px;"></td>
<td class="col-lg-2"><select name="remarks[]" class="remarks" style="width:100%;"></td>
<td><a href='javascript:void(0);' class='remove'><span class='glyphicon glyphicon-remove'></span></a></td>
</tr>
</table>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-lg-12 col-md-12">
<div class="form-group">
<label>Doctor Advice</label>
<textarea name="docadvice" id="docadvice" class="form-control"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-lg-12 col-md-12">
<div class="form-group">
<button type="submit" class="form-control btn btn-success">Save Template</button>
<input type="hidden" name="doc_id" value="{{Auth::user()->doc_id}}">
</div>
</div>
</div>
</form>
这是我的jQuery代码:
$('#addMore').on('click', function() {
$(".meditype").select2('destroy');
$(".medicine").select2('destroy');
$(".remarks").select2('destroy');
$(".duration").select2('destroy');
$(".dosage").select2('destroy');
var data = $("#tb tr").last().clone(true).appendTo("#tb");
data.find("input").val('');
data.find("select").val('');
我是控制器代码的菜鸟。那么我怎样才能在控制器中获取我所有表单元素的值。包括那些是使用 jquery 动态创建的。我尝试了很多次,但只得到空值或第一行的值。在 jquery 中,我在表单加载时初始化我的 select2 元素,并在克隆后重新初始化它们。所以当我克隆它时它工作正常。它可能对其他人有用,因为我搜索了这个但在互联网上找不到。但是现在在控制器中获取这种形式的值让我很头疼。从最近 3 天开始尝试,但没有工作。 更新 这是我正在尝试检查的代码:
$count=count($request->get('medicine'));
$temppris->medicines=$count;
$medicine=$request->get('medicine');
foreach($medicine as $index => $value)
{
$tempmedicine=new tempmedicine;
$tempmedicine->medicine_dosage=$medicine[$index]=>$value;
$tempmedicine->save();
}