Jquery - View comment data

4

I am trying to return comments data in JQuery and to end a form to enter more comments.

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"type="text/javascript"></script>
<script type="text/javascript">
$(function() {
    $("#exibir-comentario").click(function(){

          var valor = $("#exibir-comentario").attr('rel');
          var dados = $("#resposta-form"+valor+"")
          $.ajax({
              type: "POST",
              url: "comentarios.asp?id_questao="+valor+"",
              success: function(resposta) {
                  dados.html(resposta);
              },
              beforeSend: function(){
                dados.html('Buscando...');
              },
              error: function(data) {
                  dados.html('Erro ao buscar!');
              }
        });
      return false;
      });
});
</script>
</head>
<body>
<%questao=1%>
  <a id="exibir-comentario" rel="<%=questao%>" href="#">Exibir fomulário</a>
  <div id="resposta-form<%=questao%>"></div>
<%questao=2%>
  <a id="exibir-comentario" rel="<%=questao%>" href="#">Exibir fomulário</a>
  <div id="resposta-form<%=questao%>"></div>
</body>
</html>

But the second link does not return.

I solved by putting the dynamic id and making the class receive the click.


//carrega comentarios
$(function() {
    $("a.link").click(function(){
          var valor = $(this).attr('id');
          var dados = $("#resposta-form"+valor+"")
          $.ajax({
              type: "POST",
              url: "comentarios.asp?id_questao="+valor+"",
              success: function(resposta) {
                  dados.html(resposta);
              },
              beforeSend: function(){
                dados.html('Buscando...');
              },
              error: function(data) {
                  dados.html('Erro ao buscar!');
              }
        });
      return false;
      });
});

The following has now occurred. I was able to bring the form and the comments. I'm trying to send the data from the form doing exactly as I did with the previous script, but as the form is already loaded, it will not.

    
asked by anonymous 27.06.2018 / 15:18

1 answer

2

Your problem seems to be in IDs , you are using the same id for both objects and only one of them is being recognized by the script.

Uses more or less like this:

<%questao=1%>
  <a id="exibir-comentario-a" rel="<%=questao%>" href="#">Exibir fomulário</a>
  <div id="resposta-form<%=questao%>"></div>
<%questao=2%>
  <a id="exibir-comentario-b" rel="<%=questao%>" href="#">Exibir fomulário</a>
  <div id="resposta-form<%=questao%>"></div>

You'll also need to change your jquery to understand and handle the two IDs.

    
27.06.2018 / 15:57