Jquery - Display comment data

Asked

Viewed 180 times

3

I’m trying to return in Jquery comment data and end a form to add 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 fixed 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;
      });
});

Now the following has occurred. I was able to bring the fomulário and the comments. I am trying to send the form data doing exactly as I did with the previous script, but as the form is already loaded, it will not.

1 answer

1

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

It goes something 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 will also need to change your jquery to understand and treat the two Ids.

  • put a variable to receive this id before executing the function.

  • Thinking about it...only enters the function by clicking, I can not do this.

  • 1

    I solved it, put the dynamic id and clicked the class.

Browser other questions tagged

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