2014-10-04 48 views
1

我已經宣佈了相同的結構:的onclick上引導單選按鈕不起作用

<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'被按下時,沒有任何反應。

你能幫我找出錯誤嗎?

謝謝。

+0

使用'$(文件).ready'或'$(窗口).load' – 2014-10-04 14:55:24

+0

你有jQuery的安裝 – loveNoHate 2014-10-04 14:56:41

+0

http://jsfiddle.net/xpavghyb/ – 2014-10-04 14:57:33

回答

2

好了,想通了感謝this

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

您單擊標籤,而不是價值。嘗試

$(window).ready(function() { 

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

嘗試在JSFiddle