obtain value of a td by means of an onchange select in another td within the same tr in a foreach

0

I have this table created with the% co_from% a% co_from%

is used
<table class="table table-bordered">

              <tbody>
                @foreach($usuarios as $key => $tag)
                  <tr id="caputracorreo" data-userid="{{$tag->email}}">
                    <td>{{ ++$key }}</td>
                    <td>{{ $tag->name }}</td>
                    <td>{{ $tag->email}}</td>


                    <td> <select class="form-control" id="estadoactualiza" name="estadoactualiza" data-userid="{{$tag->email}}" onchange="actualizar(this)">
                      @foreach($estados as $estado)
                        @if ($estado->id == $tag->id_estado)
                         <option selected value="{{$estado->id}}">{{$estado->nombre}}</option>
                        @else
                        <option value="{{$estado->id}}">{{$estado->nombre}}</option>
                         @endif
                      @endforeach
                      {{ csrf_field() }}
                    </select>
                  </td>
                  </tr>
                @endforeach
              </tbody>
            </table>

            {{ $usuarios->links('vendor.pagination.paginadorusuarios') }}

and I have this js with the action of laravel 5.3

<script type="text/javascript">   
 function actualizar() {
 var id_estado = document.getElementById("estadoactualiza").value;
 var userid = $(caputracorreo).data('userid');
 alert (userid);
 }
</script>

You want to get the foreach previous% of onchange , but always return the first email, let's say the first td mail.

    
asked by Ulises Núñez Moreno 25.11.2016 в 16:49
source

2 answers

1

Ok. First, an ID is unrepeatable (or at least it should be), therefore you must make your ids unique to be able to reference them:

<table class="table table-bordered">

              <tbody>
                $index = 0;
                @foreach($usuarios as $key => $tag)
                  <tr id="caputracorreo" data-userid="{{$tag->email}}">
                    <td>{{ ++$key }}</td>
                    <td>{{ $tag->name }}</td>
                    <td>{{ $tag->email}}</td>


                    <td> <select class="form-control" id="estadoactualiza{{ $index }}" name="estadoactualiza" data-userid="{{$tag->email}}" onchange="actualizar({{ $index }})">
                      @foreach($estados as $estado)
                        @if ($estado->id == $tag->id_estado)
                         <option selected value="{{$estado->id}}">{{$estado->nombre}}</option>
                        @else
                        <option value="{{$estado->id}}">{{$estado->nombre}}</option>
                         @endif
                      @endforeach
                      {{ csrf_field() }}
                    </select>
                  </td>
                  </tr>
                @endforeach
              </tbody>
            </table>

            {{ $usuarios->links('vendor.pagination.paginadorusuarios') }}

and I have this js with the action of onchange

<script type="text/javascript">   
 function actualizar(numberid) {
 var id_estado = document.getElementById("estadoactualiza"+numberid).value;
 var userid = $(caputracorreo).data('userid');
 alert (userid);
 }
</script>

The dynamic id is done with a counter, that id you pass as a parameter in your function and use it within it.

    
answered by 25.11.2016 / 20:28
source
0

When assigning the id of the select that you want to obtain, you must have a unique name, perhaps assigning the identifier of each row. For example:

id="estadoactualiza_{{ $key }}"

Then, in the onchange event, the same identifier passes

onchange="actualizar({{ $key }})"

In your update () function you will now have to look for the item that you just made unique:

<script type="text/javascript">   
  function actualizar(idunico) {
    var id_estado = document.getElementById("estadoactualiza_" + idunico).value;
    /* El resto de tu función va aquí... */
  }
</script>

I hope it serves you.

    
answered by 25.11.2016 в 20:11