How do I manipulate data from a select created from jquery

Asked

Viewed 361 times

1

I am trying to take the values of a select that I entered through a jquery, and as the value it adds elements within a div also created by that same jquery. The problem is that I’m not getting these values.

This is the code where I insert select into a div created in html.

<!-- Jquery Add Formulario -->
<script>

$(function AddForm() {
    var adicionar = $('.grid');
        $(document).on('click', '#id_AddMais', function () {
            $('<div  class="questionario w3-col m11 w3-white w3-padding-large">'+
              '<form method="POST" action="">'+

                  '<div class="w3-half">'+
                    '<input id="id_Enunciado" class="w3-input" type="text" name="txtEnunciado" placeholder="Enunciado">'+
                  '</div>'+

                  '<div class="styled-select w3-light-grey w3-right slate">'+

                  '<select id="id_FormaResposta" name="formaResposta">'+

                  '<optgroup>'+
                    '<option value="respostaCurta">Resposta curta</option>'+
                    '<option value="paragrafo">Parágrafo</option>'+
                  '</optgroup>'+

                  '<optgroup>'+
                    '<option value="multiplaEscolha">Múltipla escolha</option>'+
                    '<option value="caixasDeSelecao">Caixas de seleção</option>'+
                    '<option value="listaSuspensa">Lista suspensa</option>'+
                  '</optgroup>'+

                  '<optgroup>'+
                    '<option value="uploadArquivo">Upload de arquivo</option>'+
                  '</optgroup>'+

                  '<optgroup>'+
                    '<option value="escalaLinear">Escala linear</option>'+
                    '<option value="gradeME">Grade de múltipla escolha</option>'+
                  '</optgroup>'+

                  '<optgroup>'+
                    '<option value="data">Data</option>'+
                    '<option value="horario">Horário</option>'+
                  '</optgroup>'+

                  '</select>'+
                '</div>'+

                '<input id="id_Suporte" class="w3-input" type="text" name="txtSuporte" placeholder="Suporte">'+

                '<input id="id_Comando" class="w3-input" type="text" name="txtComando" placeholder="Comando">'+

              '</form><br>'+

              '<div class="divResposta">'+

              '</div><br>'+

              '<button  onclick="" id="id_Excluir" class="w3-bar-item w3-button w3-right" style="color: grey"><i class="material-icons">delete</i></button>'+
              '<button  onclick="" id="id_Copiar" class="w3-bar-item w3-button w3-right" style="color: grey"><i class="fa fa-copy"></i></button>'+


            '</div>').appendTo(adicionar);
            return false;
       });

       $(document).on('click', '#id_Excluir', function () {
             $(this).parents('.questionario').remove();
               return false;
       });
});
</script>

Here I am "taking" the value of select and trying to insert elements inside the previously created div(divResposta).

<!-- Pegar valor select -->
<script>

$(document).ready(function(){

$('#id_FormaResposta').on('change', function(){

    var valorSelect = $(this).val();
    var modificarDiv = $('.divResposta');

    if(valorSelect == 'paragrafo'){

        $(
            '<input type="text" value="" placeholder="Paragrafo" disabled >'

        ).appendTo(modificarDiv);
        return false;

    }

});

});

</script>

If anyone can help me, I’d appreciate it.

  • Are you sure there’s only one id '#id_FormaResposta' all over the page? Try to delegate like this: $(document).on('change', '#id_FormaResposta', function(){

  • Even enter the select change?

1 answer

0


I don’t know the sequence of the HTML additions on your page, but assuming you defined the events and then called: Addform, which added the elements, the change event will not be triggered, because it was delegated before the element was rendered. So let’s switch the assignment to Document.

I suggest that, since you add several forms, do not use the id for select, which would define a single element, but a class. And your code would look like:

$(document).on('change', '.FormaResposta', function(){
    var valorSelect = $(this).val();
    var modificarDiv = $(this).parents(".questionario").find('.divResposta');

    if(valorSelect == 'paragrafo'){
        $('<input type="text" value="" placeholder="Paragrafo" disabled >').appendTo(modificarDiv);
        return false;
    }
});
  • Thanks that way worked out.

  • However, since the first jquery makes it possible to create several selects, they always have the same values, so this is adding this input in the case of the two selects created.. How do I fix it? (I don’t know if I understand well)

  • You shouldn’t have more than one element with the same ID, that’s why this is happening. For every time he clicks: addMais, you need to replicate all that HTML too, @Wolf?

  • I don’t know if there’s any other way to do it, but it’s just that since it’s possible to fill out as many forms as you want with the same information, that’s the way I found it...

  • I updated the answer, @Wolf.

  • Thank you very much, now it’s working right..

  • @Wolf, if the answer helped you and you have no more doubts, can mark as accepted so that other people can be helped too.

Show 2 more comments

Browser other questions tagged

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