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;
– Marcus Italo
It didn’t work out, he keeps taking only the value of the first option, but still thanks for the help
– matheus gomes
What value are you willing to return??
– Oliveira
I would like you to return the value of the selected option, if option 2 is selected, return value 2
– matheus gomes
Change the onchange by placing a
this
as a parameter:onchange="grupoModal(this, \'viewGrupoModal\',\'sp_grupoInput\')"
... and in the function take the value withvar grupo = valor.value;
... and put the parameter in the tb function:function grupoModal(valor, acao, id) {
– Sam