最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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'); 
    });
});
发布评论

评论列表(0)

  1. 暂无评论