javascript - 输入类型“收音机”jQuery上的单击事件
问题描述
无法将事件分配给单选按钮。我用谷歌搜索了这个问题,有很多不同的解决方案,但没有任何帮助。
我尝试的最后一个解决方案是:
$(document).ready(function() {
$("#monthly input[name=pricing-1]").click(function() {
console.log("Test");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-round btn-outline-primary w-150 active">
<input id="monthly" name="pricing-1" value="monthly" autocomplete="off" checked="" type="radio"> Monthly
</label>
</div>
但是当我单击按钮时没有任何反应。怎么了?
解决方案
问题:
主要问题来自选择器#monthly input[name=pricing-1]
,它将搜索名称pricing-1
为 id 的元素的子元素的输入monthly
。
解决方案:
您必须使用$("input[name=pricing-1]")
or$("#monthly")
作为选择器,因为两者都引用相同的元素:
$(document).ready(function() {
$("#monthly").click(function() {
console.log("Test 1");
});
$("input[name=pricing-1]").click(function() {
console.log("Test 2");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-round btn-outline-primary w-150 active">
<input id="monthly" name="pricing-1" value="monthly" autocomplete="off" checked="" type="radio"> Monthly
</label>
</div>
推荐阅读
- c# - 如何使用 lambda 表达式从列表中获取继承的对象成员
- git - 如何为具有私有 git 子模块的私有仓库设置 GitHub Actions CI 服务?
- variables - 并行 Jenkinsfile 任务中的局部变量
- reactjs - 如果组件中不存在 redux 存储中的值,则重定向用户
- html - 使用视频标签html5时如何在移动设备上显示视频缩略图
- delphi - Indy TCP SSL 多线程
- c - 如何使用 atmega 退出 USART ISR?
- laravel - 如何使用 froala 编辑器上传图片?
- laravel - 断言模拟期望已经过去的正确方法
- android - 如何从相机逐帧捕获而不在Android上保存视频?