简体   繁体   English

jQuery事件(提交时)正在影响我的所有div,而不是唯一一个单击了按钮的div?

[英]jquery event (on submit) is affecting all my divs and not the only one where the button was clicked?

what's up guys? 你们好吗? look... I have a comment system for my web page... and I've been dealing with this little problem for a entire week. 看起来...我的网页有一个评论系统...而且我整整一周都在处理这个小问题。 I really need some help here x_x ... the thing is that when a user leave a comment on my page, this comment is showed automatically thanks to ajax, that's ok... 我在这里确实需要一些帮助x_x ...问题是,当用户在我的页面上留下评论时,由于ajax,该评论会自动显示,没关系...

each comment can be voted. 每个评论都可以投票。 and here's my problem... these divs that contain the forms for voting are build dynamically and the thing is that when I do click on the button for sending the form in any comment... the resulting data appears in all the comments! 这是我的问题...这些包含投票表格的div是动态构建的,事实是当我确实单击任何评论中发送表格的按钮时...结果数据出现在所有评论中! instead of appear in the specific one where the submit button was clicked, so I don't know what to do at this point, I hope you can give me a hand. 而不是出现在单击提交按钮的特定按钮中,所以我目前不知道该怎么办,希望您能帮上忙。 this is my code 这是我的代码

the form: 表格:

<label > Vote </label>
<form action="vote.php" method="POST" class="form-vote" id="form-vote">
<button class="icon-thumbs-up" id="icon-thumbs-up"></button>
<input hidden="hidden" type="text" name="num-comment" id="num-comment" value="'.$d['id'].'" >
<input  hidden="hidden" type="text" name="point" id="point" value="'.$d['point'].'" >
<p id="actual-points" class="actual-points"> '.$d['point'].'</p> 
<div id="result"  class="result"> </div>
</form>

the script: 剧本:

<script type="text/javascript">
$(document).ready function() {

    $('.form-vote')on('submit', function() {

        $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            data: $(this).serialize(),
            success: function(data) {

                                $('.actual-points').hide();
                                $('.result').html(data).fadeIn('slow');
            }
        })
        return false;
    }); 
})  
</script>

Have you tried saving the 'this' object of the original event and using it inside the success function like this: 您是否尝试过保存原始事件的'this'对象,并在成功函数中使用它,如下所示:

$('.form-vote')on('submit', function(e) {
  e.preventDefault();
  var $form = $(this); // Save here

  $.ajax({
    type: 'POST',
    url: $(this).attr('action'),
    data: $(this).serialize(),
    success: function(data) {
      // use here
      $form.find('.actual-points').hide();
      $form.find('.result').html(data).fadeIn('slow');
    }
  })
  return false;
}); 

change this: 改变这个:

$('.result').html(data).fadeIn('slow');

to this: 对此:

$(this).find('.result').html(data).fadeIn('slow');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM