Only the first select works (Popular selects with Parent element php javascript)

Asked

Viewed 53 times

0

I am developing a code to renew student enrollments, on this page the first select the user will select whether the student will go to the next class or will remain in it. I looped in php and is loading all students:

inserir a descrição da imagem aqui

I got popular the selects, however, only works of the first student, the others. I’ve been searching and I believe I should create element Parent and Child in my javascript but I tried everything and could not.

I followed the code of the first select of the image:

<select 
  class="form-control selectboxit" 
  name="promotion_status_<?php echo $row['student_id'];?>" style="width: 40px;" id="select1">
  <option value="vazio">Selecione uma opção</option>
  <option value="<?php echo $class_id_to;?>">
  <?php echo 'Renovar para' ." - ". $this->crud_model->get_class_name($class_id_to); ?>
  </option>
  <option value="<?php echo $class_id_from; ?>" >
  <?php echo 'Permanecer no' ." - ". $this->crud_model->get_class_name($class_id_from); ?>
 </select>

I followed the second select code of the image:

<select id="select2">
 <option value="">selecione</option>
</select>

Javascript code:

$("#select1").change(function() {
    var proxima_turma = <?php echo json_encode($proxima_turma); ?>;
    var antiga_turma = <?php echo json_encode($antiga_turma); ?>;

    var valor = $(this).val();

    if (valor == "vazio") {
        $("#select2").html("");
        {
            $("#select2").append("<option value='test'> Selecione </option>");
        }

    }else if (valor == "<?php echo $class_id_to;?>") {
        $("#select2").html("");

        for(var i = 0; i < proxima_turma.length; ++i) {
            $("#select2").append("<option value='" + proxima_turma[i]['section_id'] + "'>" + proxima_turma[i]['name'] + "</option>");
        }


    } else if (valor == "<?php echo $class_id_from; ?>") {

        $("#select2").html("");

        for(var i = 0; i < antiga_turma.length; ++i) {
            $("#select2").append("<option value='" + antiga_turma[i]['section_id'] + "'>" + antiga_turma[i]['name'] + "</option>");
        }
    }
});

});

How to define Parent and Child elements?

How is the table with the selects currently:

<tr>
 <td align="left">
    <?php
     if((!is_null($transferidos) && (!is_null($evadidos)))){
      echo $name . ' - ' .'<b></b>Não pertencente a unidade de ensino';
         }elseif((!is_null($transferidos) || (!is_null($evadidos)))){
         echo $name . ' - ' .'<strong>Não pertencente a unidade de 
 ensino</strong>';
  }else{
 echo $name;
 }
 ?>
 </td>
 <td align="center">
 <?php if($row['section_id'] != '' && $row['section_id'] != 0)
 echo $this->db->get_where('section' , array('section_id' => 
 $row['section_id']))->row()->name;
  ?>
 </td>
 <td>
 <?php if($query->num_rows() < 1):?>
  <select class="select1"   name="promotion_status_<?php echo 
  $row['student_id'];?>" >
  <option value="vazio">Selecione uma opção</option>
  <option value="<?php echo $class_id_to;?>">
  <?php echo 'Renovar para' ." - ". $this->crud_model- 
 >get_class_name($class_id_to); ?>
</option>
<option value="<?php echo $class_id_from; ?>" >
<?php echo 'Permanecer no' ." - ". $this->crud_model- 
>get_class_name($class_id_from); ?>
</select>
 <?php endif; ?>
 <?php if($query->num_rows() > 0): ?>
 <button class="btn btn-success">
 <i class="entypo-check"></i> Matricula já foi renovada
 </button>
 <?php endif;?>
 </td>
 <td>
     <?php if($query->num_rows() < 1):?>
     <select class="select2" name="promotion_status2_<?php 
    echo $row['student_id'];?>">
     <option value="">selecione</option>
         </select>
     <?php endif;?>
     <?php if($query->num_rows() > 0):?>
     <button class="btn btn-success">
     <i class="entypo-check"></i> Matricula já foi renovada
     </button>
     <?php endif;?>
     </td>
     </tr>
  • You are putting #select1 and #Select2 for all selects or created individual ids for each one?

  • You cannot repeat id’s. You will only get the first one.

  • Thiago Costa - Individual for each one.

  • Sam - How to catch the first for the whole loop?

1 answer

1


You cannot repeat id’s on the same page as you will only get the first one you find.

Use class instead of id. Just change id="select1" and id="select2" for class="form-control selectboxit select1" and class="select2" in the selects of the list.

Then you will change the .select2 which is on the same line as .select1 when the change:

$(".select1").change(function() {
    var proxima_turma = <?php echo json_encode($proxima_turma); ?>;
    var antiga_turma = <?php echo json_encode($antiga_turma); ?>;

    var valor = $(this).val();
    var select2 = $(this).closest("tr").find(".select2"); // busca o select2 da mesma linha

    if (valor == "vazio") {
        select2
        .html("")
        .append("<option value='test'> Selecione </option>");

    }else if (valor == "<?php echo $class_id_to;?>") {
        select2.html("");

        for(var i = 0; i < proxima_turma.length; ++i) {
            select2.append("<option value='" + proxima_turma[i]['section_id'] + "'>" + proxima_turma[i]['name'] + "</option>");
        }


    } else if (valor == "<?php echo $class_id_from; ?>") {

        select2.html("");

        for(var i = 0; i < antiga_turma.length; ++i) {
            select2.append("<option value='" + antiga_turma[i]['section_id'] + "'>" + antiga_turma[i]['name'] + "</option>");
        }
    }
});
  • Sam almost worked, fail mine. If I put:<select class="Select2" name="promotion_status2_<? php echo $Row['student_id'];? >"> <option value="">select</option> </select> The second select does not appear, it is hidden, but if I remove the class it appears but does not happen anything.

  • If the second select already has a class, you just add the class select2, guy: class="outras_classes_que_já_tem select2"

  • No, I will edit the question and at the end put the whole table as it is currently.

  • Do not remove the other classes from the first select. It should look like this: class="form-control selectboxit select1"

  • Good luck there. QQ thing just call.

  • Sam, somehow the javascript code is overwriting css Select2, leaving it with display: None.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.