繁体   English   中英

输入类型'radio' jQuery上的点击事件

[英]Click event on input type 'radio' 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]")$("#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>

试试这个:在你的输入上添加 onclick="name_func"

<input onclick="test()" id="monthly" name="pricing-1" value="monthly" autocomplete="off" checked="" type="radio"> Monthly

在你的 JS 中:

function test() { console.log("test"); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM