【问题标题】:onclick on Bootstrap radio button does not work在 Bootstrap 单选按钮上的 onclick 不起作用
【发布时间】:2014-10-04 14:53:18
【问题描述】:

我已经声明了相同的结构:

<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>

根据this 应该使我能够通过添加来附加onclick 事件:

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

JQuery 已经包含在代码中。

但是,当按下 `#past5m' 时,什么也没有发生。

你能帮我找出错误吗?

谢谢。

【问题讨论】:

标签: javascript jquery html twitter-bootstrap


【解决方案1】:

您单击的是标签,而不是值。试试

$(window).ready(function() {

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

试试JSFiddle

【讨论】:

    【解决方案2】:

    好的,感谢this:

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

    【讨论】:

      猜你喜欢
      • 2015-11-15
      • 2014-10-30
      • 2018-10-12
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2013-03-09
      相关资源
      最近更新 更多