ERaufi
ERaufi

Reputation: 1663

change multiple dropdowns value inside a loop

Hello Guys I'm new to laravel and Jquery. how to change multiple drop downs value which are generated via a foreach loop based on received values

I've used jquery but it only changes the first drop down value and won't change the rest

my view

@extends('layouts.app')

@section('content')
   <!-- Modal for adding new request-->
                    <form method="post" action="insertrequest">
                        @csrf
                        <table id="formTable" >
                            <thead>
                            <td>مقدار</td>
                            <td>واحد</td>
                            <td>شرح</td>
                            <td>ملاحظات</td>
                            </thead>
                            @foreach($result as $results)
                            <tr>
                                <td><input type="text" class="form-control" name="quantity[]" id="quantity" placeholder="مقدار" value="{{$results->quantity}}"></td>
                                <td><select name="units_id[]" id="units_id">
                                        @foreach($unit as $units)
                                            <option value="{{$units->id}}">{{$units->unit_name}}</option>
                                        @endforeach
                                    </select></td>
                                <td><input type="text" class="form-control" name="description[]" id="description" value="{{$results->description}}"></td>
                                <td><input type="text" class="form-control" name="remarks[]" id="remarks" value="{{$results->remarks}}"></td>
                            </tr>
                                <script>
                                    $("#units_id").closest("tr",document.getElementById("units_id").value = "{{$results->units_id}}");
                                </script>
                            @endforeach
                        </table>
                        <input type="submit" class="btn btn-primary" value="submit">
                    </form>
@endsection

enter image description here

Upvotes: 0

Views: 151

Answers (1)

iLaxo Mit
iLaxo Mit

Reputation: 87

I have reviewed your code Issue is in select tag line You have to set unique id for each select tag dropdown. There is multiple select tag is generating so every time 1st occurance units_id will taken by javascript so you have to set unique id

So create dynamic id it works...

Upvotes: 1

Related Questions