javascript - onclick on Bootstrap radio button does not work - Stack Overflow

I have declared the same structure:<div class="btn-group" data-toggle="buttons"

I have declared the same structure:

<div class="btn-group" data-toggle="buttons" style="margin-left:50px;"> 
    <label class="btn btn-default">
        <input id="past5m" type="radio"> 5m
    </label> 
    <label class="btn btn-default"> 
        <input id="past1h" type="radio"> 1h
    </label>
</div>

which, according to this should enable me to attach an onclick event by adding:

$(document).ready(function() {
    $('#past5m').click(function() { 
        alert('hello'); 
    });
});

JQuery is already included in the code.

However, when `#past5m' is pressed, nothing happens.

Can you please help me to spot the error?

Thanks.

I have declared the same structure:

<div class="btn-group" data-toggle="buttons" style="margin-left:50px;"> 
    <label class="btn btn-default">
        <input id="past5m" type="radio"> 5m
    </label> 
    <label class="btn btn-default"> 
        <input id="past1h" type="radio"> 1h
    </label>
</div>

which, according to this should enable me to attach an onclick event by adding:

$(document).ready(function() {
    $('#past5m').click(function() { 
        alert('hello'); 
    });
});

JQuery is already included in the code.

However, when `#past5m' is pressed, nothing happens.

Can you please help me to spot the error?

Thanks.

Share Improve this question edited May 23, 2017 at 12:02 CommunityBot 11 silver badge asked Oct 4, 2014 at 14:53 Eleonora CiceriEleonora Ciceri 1,7983 gold badges17 silver badges34 bronze badges 3
  • use $(document).ready or $(window).load – Pranav C Balan Commented Oct 4, 2014 at 14:55
  • Do you have jquery installed – loveNoHate Commented Oct 4, 2014 at 14:56
  • jsfiddle/xpavghyb – Pranav C Balan Commented Oct 4, 2014 at 14:57
Add a ment  | 

2 Answers 2

Reset to default 3

You click the label, not the value. Try

$(window).ready(function() {

    $('#past5m').closest('label').click(function() { 
        alert('hello'); 
    });
});

Try it on JSFiddle

Ok, figured it out thanks to this:

$(document).ready(function() {
    $('#past5m').change(function() { 
        alert('hello'); 
    });
});

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信