您能否看一下下面的代码,让我知道为什么我不能使用this.id
来获取所选收音机的ID?
<div id="pay" class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="mode" id="option1"> Cash
</label>
<label class="btn btn-primary">
<input type="radio" name="mode" id="option2"> Cheque
</label>
</div>
<script>
$("#pay input:radio").on('click',function(){
alert(this.id);
});
</script>
谢谢
最佳答案
如果实际上发布的代码就是整个jQuery,您就已经忘记使用文档就绪处理程序了:
$(document).ready(function(){
// your code should be in here
});
或者,您可以确保将
<script>
放在结束</body>
标记之前,这两种方法都可以确保在尝试绑定事件处理程序之前,已加载页面的内容并显示在页面中。