2010-10-05 44 views
-1

我有这样的jQuery代码工作正常,但在最后的影像是不会改变到我这里指定的SRC:更改jquery中的图像?

的jQuery:

$(document).ready(function() { 
     $("a.vote_up").click(function(){ 
     //get the id 
     var the_id = $(this).attr('id'); 

     //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(); 
// my problem is here 
      $(".vote_up#" + the_id + " img").attr("src", "img/upvoteActive.png"); 
      } 
     }); 
     }); 
    }); 

html代码:

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

回答

4

不要与ID一起使用class;它是多余的,因为ID应该永远是独一无二的......

$("#" + the_id + " img").attr("src", "img/upvoteActive.png"); 

而且,你不能在ID属性中的字符$。引述W3C on the ID attribute ...

ID和名称标记必须以 字母开头([A-ZA-Z]),并且可以通过任意数量的字母之后 ,数字 ([0-9 ]),连字符(“ - ”),下划线 (“_”),冒号(“:”)和句点 (“。”)。

+0

虽然这仍然deosnt工作! :((对不起 – getaway 2010-10-05 19:01:23

+2

+1;只是因为它不适用于你,所以没有必要投票,因为答案仍然是一个好的点 – Mottie 2010-10-05 19:06:24

+1

但它不相关,他应该使用评论系统,它不是一个答案,它的建议!!! – getaway 2010-10-05 19:07:17

1

看起来你多次使用相同的ID(img和count)。尝试使ID更独特,如:

<a href='#' class='vote_up' id="$id_link"><img src="img/uparrow.png" /></a> 
<span class="vote_count" id="$id_count"></span> 
+0

欢呼它的作品 – getaway 2010-10-05 19:07:43

+2

你不能做出更独特的东西,它可以是独特的......或不是。 – 2011-04-05 02:34:37