Problems with onChange and ajax

Asked

Viewed 150 times

0

Good morning, I am programming a system in PHP and JS, putting it in the index works normal, but I need to put in a modal and in this the onchange Buga, taking only the value of the first option.

function selectGrupo(){
  include("conexao.php");
  $saida ='
  <div class="input-group">
    <div class="input-group-btn">
      <button class="btn btn-success add-more" type="button" onClick="addSoftware(\'sp_software\',\''.$grupo.'\')">
        <i class="glyphicon glyphicon-plus"></i>
      </button>
    </div>
    <select class="form-control" id="grupoSelectModal" onchange="grupoModal(\'viewGrupoModal\',\'sp_grupoInput\')">';

    $query = "SELECT id, nomeGrupo, descricao FROM grupos";
    $query = mysqli_query($conn,$query);
    $reg = mysqli_fetch_array($query);
    while ($reg == 0) {
      echo "deu ruim";
      exit;
    }
    while ($reg != 0) {
      $id = $reg['id'];
      $nomeGrupo = $reg['nomeGrupo'];
      $saida .= '<option value="'.$id.'"> '.$nomeGrupo.' </option>';
      $reg = mysqli_fetch_array($query);
    }

    $saida .= '
    </select>
  </div>';
  echo $saida;
}

function grupoModal(acao, id) {
  var grupo = document.getElementById('grupoSelectModal').value;
  alert(grupo);
  window.status = "alterando";
  $.ajax({
    url: '../funcoes/funcoes.php',
    type: 'POST',
    dataType: 'html',
    data: 'acao=' + acao + '&grupo=' + grupo + '&status=' + status,
    success: function(data) {
      alert("Teste de success");
      $('#' + id).empty();
      $("#" + id).append(data);
    },
    error: function(data) {
      alert('Erro ao alterar o registro! Tente novamente...' + data);
    },
  });
}

  • I believe you have to change the following section: var group = Document.getElementById('groupSelectModal'). value; by var group = Document.getElementById("groupSelectModal"). options[Document.getElementById("groupSelectModal"). selectedIndex]. value; This way it will "catch" the value of the selected <option> value;

  • It didn’t work out, he keeps taking only the value of the first option, but still thanks for the help

  • What value are you willing to return??

  • I would like you to return the value of the selected option, if option 2 is selected, return value 2

  • Change the onchange by placing a this as a parameter: onchange="grupoModal(this, \'viewGrupoModal\',\'sp_grupoInput\')"... and in the function take the value with var grupo = valor.value;... and put the parameter in the tb function: function grupoModal(valor, acao, id) {

No answers

Browser other questions tagged

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