您能否看一下以下代码,并告诉我为什么我无法使用 this.id
获取所选 radio 的 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>
标记,这两种方法都确保在您尝试绑定(bind)事件处理程序之前加载页面内容并显示在页面中。
关于javascript - 在 Div 内选择输入的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23838379/