1

我在我的 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();
        }
4

4 回答 4

0

你试过这个吗

 $medicine=$request->get('medicine');
    foreach($medicine as $index => $value) 
    {
        $data = $value['medicine']; //here name will be your stored medicine name
        print_r($data);
        exit();
    }
于 2018-11-24T13:20:58.487 回答
0

首先dd将 store 函数中的函数设为dd($request->medicine);

请发布结果然后我会告诉你是什么原因

于 2018-11-24T09:51:50.737 回答
0

感谢您的回复尝试在克隆的元素中添加药物并使用相同的dd($request->medicine);方法,如果你得到数组中的所有值,它工作得很好但是如果你只得到原始 div 元素的值,它肯定是它的命名问题

请通过填充数组将结果发布如下,以便我查看

for Example

array:2 [▼
  0 => "2"
  1 => "3"
]
于 2018-11-24T10:57:16.300 回答
0

我知道这个朋友。在关闭我的表单中的 div 之前放置我的<form>标签并关闭它只是一个愚蠢的错误。在浏览器控制台中显示不匹配标签的警告错误,但我只是忽略它。为这个愚蠢的错误浪费了 3 天时间。每次使用表单时,您都应该检查此类警告。我只是将我的表单标签放在我制作标签的 div 的开头..希望这对其他人有帮助..

于 2018-11-25T17:45:57.697 回答