在开发过程中,有时候我们需要选中表单,当选中表单的时候,进行某些操作,有几种方法,下面我来介绍一下:
jquery中控制单选按钮的方法(方法一)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Example 2</title> <style> .cb-container { width: 100%; text-align: center; padding-top: 20px; } .cb-gap { padding-left: 20px; padding-right: 20px; margin-left: 20px; } .cb-gap2 { margin-left: 20px !important; } </style> </head> <body> <div class="cb-container"> <input type="radio" class="cb-radio" id="r1" name="rd"/> <input type="radio" class="cb-radio cb-gap2" id="r2" name="rd"/> <button id="btn" type="button" class="btn btn-primary cb-gap">left</button> <button id="btn2" type="button" class="btn btn-primary cb-gap">right</button> </div> <script type="text/javascript"> $(document).ready(function () { var radios = $(".cb-radio"); $("#btn").click(function () { radios.eq(0).prop("checked", true); radios.eq(1).prop("checked", false); }); $("#btn2").click(function () { radios.eq(0).prop("checked", false); radios.eq(1).prop("checked", true); }); }); </script> </body> </html> |
7