javascript - Link calling a jQuery function doesn't seem to be working - Stack Overflow

I am a jQuery newb so this is probably rudimentary.I have a test page here:where I am trying to make

I am a jQuery newb so this is probably rudimentary.

I have a test page here: where I am trying to make links to vote up or down.

Here is how the links look like:

<a class="link_button" id="vote_up">Vote Up</a> 
<a class="link_button" id="vote_down">Vote Down</a>

I use the class attribute for styling all over the site with that link_button so I decided to use the id attribute to distinguish the link for the jQuery call.

Here is how my jQuery function looks like:

$('a.vote_up').click(function() 
{
alert("up");
});

Right now I just want to make sure it is getting called properly. But the alert isn't getting called which is making me think this is broken somewhere. Any idea how I can get this working?

Thanks!

I am a jQuery newb so this is probably rudimentary.

I have a test page here: http://www.problemio. where I am trying to make links to vote up or down.

Here is how the links look like:

<a class="link_button" id="vote_up">Vote Up</a> 
<a class="link_button" id="vote_down">Vote Down</a>

I use the class attribute for styling all over the site with that link_button so I decided to use the id attribute to distinguish the link for the jQuery call.

Here is how my jQuery function looks like:

$('a.vote_up').click(function() 
{
alert("up");
});

Right now I just want to make sure it is getting called properly. But the alert isn't getting called which is making me think this is broken somewhere. Any idea how I can get this working?

Thanks!

Share Improve this question asked Sep 30, 2011 at 20:51 GenadinikGenadinik 18.6k64 gold badges191 silver badges288 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

You're using the class selector . instead of the id selector #. Hence do either

$('#vote_up').click(function(){
     alert("up");
});

or

$('.link_button').click(function(){ 
    if( this.id === 'vote_up' )
        alert('vote up'); 
    else if ( this.id === 'vote_down' )
        alert('vote down'); 
 } 

EDIT: also make sure everything is in a $(document).ready... i.e

$(function(){ 
    $('#vote_up').click(function(){
         alert("up");
    });
}); 

Your current selector is incorrect, it is looking for an a tag with a class of vote_up not it's id...

use this instead...

$(function() {
    $('#vote_up')...
});

Have you tried the id selector?

$('#vote_up').click(function() {
    alert('up');
});

a.vote_up looks for an <a> tag with the class vote_up. In your HTML vote_up is an ID, so you want this:

$('#vote_up').click(function() 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356825a4624168.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信