2010-10-05 144 views
2

我有这个jQuery的文件,但vote_up click handler保持与vote_down click handler发生冲突,当我点击它改变了vote_up元素vote_down元素:jQuery的 - 点击一个元素上触发另一个

jQuery脚本:

$(document).ready(function() { 
    $("a.vote_up").click(function() { 
     //get the id 
     var the_id = this.id.split('_').pop(); 
     //the main ajax request 
     $.ajax({ 
      type: "POST", 
      data: "action=vote_up&id=" + the_id, 
      url: "ajax/votes.php", 
      success: function (msg) { 
       $("span.vote_count#" + the_id).html(msg).fadeIn(); 
       $("#" + the_id + " img").attr("src", "img/uparrowActive.png"); 
      } 
     }); 
    }); 
}); 
$(document).ready(function() { 
    // the vote down function 
    $("a.vote_down").click(function() { 
     //get the id 
     var vote_id = this.id.split('_').pop(); 
     //the main ajax request 
     $.ajax({ 
      type: "POST", 
      data: "action=vote_down&id=" + vote_id, 
      url: "ajax/votes.php", 
      success: function (msg) { 
       $("span.vote_count#" + vote_id).html(msg).fadeIn(); 
       $("#" + vote_id + " img").attr("src", "img/downarrowActive.png"); 
      } 
     }); 
    }); 
}); 

HTML:

<a href='#' class='vote_up' id="id_23"><img src="img/uparrow.png" /></a> 
<a href='#' class='vote_down' id="id_23"><img src="img/downarrow.png" /></a> 

了jQuery和Ajax请求工作呢罚款,但SRC的变化是问题,因为当我点击否决,我t改变了vote_up图像!

+0

所有这些都可以进入'$(document).ready()' – colinmarc 2010-10-05 23:25:56

+0

逍遥游 - 请参阅@ Pointy的回答并记住它。在您之前的问题中已经提到过很多次。这是一个不容忽视的重要规则。对不起,很生硬。 – user113716 2010-10-05 23:27:03

+0

对不起,我只是很困惑,这是我为我的大学项目,所以我必须得到它的权利,即时通讯这样的新手:)) – getaway 2010-10-05 23:28:22

回答

1

如果您正在寻找某种将事件聚焦到重复数据的策略,那么利用带附加数字的ID来引用各种元素可能会起作用,但可能不是最佳方法。

我假设每个重复的项目都有自己的重复容器。您最好在该容器上放置一个唯一的ID,并从那里找到要素。当您单击相应的向上/向下元素

<div id='outerContainer'> 
    <div id='set_123' class='someContainer'> 
     <a href='#' class='vote_up'><img src="img/uparrow.png" /></a> 
     <span class='vote_count'></span> 
     <a href='#' class='vote_down'><img src="img/downarrow.png" /></a> 
    </div> 
    <div id='set_124' class='someContainer'> 
     <a href='#' class='vote_up'><img src="img/uparrow.png" /></a> 
     <span class='vote_count'></span> 
     <a href='#' class='vote_down'><img src="img/downarrow.png" /></a> 
    </div> 
    <div id='set_125' class='someContainer'> 
     <a href='#' class='vote_up'><img src="img/uparrow.png" /></a> 
     <span class='vote_count'></span> 
     <a href='#' class='vote_down'><img src="img/downarrow.png" /></a> 
    </div> 
</div> 

你可以use .delegate()放置点击#outerContainer处理火:

拿这个举例来说。

喜欢的东西:

$(document).ready(function() { 
    $('#outerContainer').delegate('.vote_up', 'click', function() { 
     //get the id 
     var the_id = $(this).closest('.someContainer').attr('id').split('_').pop(); 
     //the main ajax request 
     $.ajax({ 
      type: "POST", 
       // Make sure "this" in the callback refers to the element clicked 
      context: this, 
      data: "action=vote_up&id=" + the_id, 
      url: "ajax/votes.php", 
      success: function (msg) { 
        // Not sure where your vote_count is. See the HTML for my placement 
       $(this).siblings("span.vote_count").html(msg).fadeIn(); 
        // get the child <img> and set its src 
       $(this).children("img").attr("src", "img/uparrowActive.png"); 
      } 
     }); 
    }); 
    $('#outerContainer').delegate('.vote_down', 'click', function() { 
     //get the id 
     var the_id = $(this).closest('.someContainer').attr('id').split('_').pop(); 
     //the main ajax request 
     $.ajax({ 
      type: "POST", 
       // Make sure "this" in the callback refers to the element clicked 
      context: this, 
      data: "action=vote_down&id=" + the_id, 
      url: "ajax/votes.php", 
      success: function (msg) { 
        // Not sure where your vote_count is. See the HTML for my placement 
       $(this).siblings("span.vote_count").html(msg).fadeIn(); 
        // get the child <img> and set its src 
       $(this).children("img").attr("src", "img/downarrowActive.png"); 
      } 
     }); 
    }); 
}); 

所以你需要数量的ID是每个.someContainer。您遍历该容器以获取该ID,并执行您的.split().pop()

然后在AJAX请求中,我设置了the context: property for $.ajax(),这样this仍然会引用您的回调中被点击的元素。

在回调中,您发现the .siblings()的类别为.vote_count,并且设置了its .html()的内容。

最后你use .children()得到img元素,并设置它的src属性。

这应该给出一般的想法。您需要调整您的HTML。

+0

感谢您的回答,我chnaged evrything以适应我的html,但我试图阅读关于上下文位,但我没有得到它,我已经发送ajax到votes.php不是我,我想我应该把这个'$(this).attr(“ id“)' – getaway 2010-10-06 00:35:34

+0

@getaway - 我不明白你的最后一句话,但就上下文而言,我将该属性添加到AJAX请求的原因是因为*在回调中*我希望'this'引用元素当你有一个AJAX回调,就像你的'成功:'回调,意思'this'在回调内部奇迹般地改变,以便它不再引用被点击的元素。为了使它再次引用该元素,可以通过请求的'context:'属性传递'this'。这只是一种方法,但可能是最干净的。 – user113716 2010-10-06 00:52:25

+0

...如果我没有这样做,'$(this).siblings(...'和'$(this).children(...')将不起作用,因为'this'不再是对于被点击的'.vote_up'或'.vote_down'元素的引用 – user113716 2010-10-06 00:54:54

5

对于两个不同的元素,不能使用相同的“id”值。

+0

所以我应该改变vote_own id到'votedown_23' – getaway 2010-10-05 23:25:42

+6

@getaway - 你可以改变它'van_halen_1984',只要它是唯一的。 :o) – user113716 2010-10-05 23:28:12

+0

如果将其更改为“id_23”以外的内容,情况会有所改善。如果您希望某些事情能够正常工作,则“id”值**必须在整个页面中唯一。这就是所谓的“身份证”(“身份证”)。 – Pointy 2010-10-05 23:28:56

相关问题