简体   繁体   English

JQuery中的ASP.NET单选按钮开关

[英]ASP.NET Radio Button Switch in JQuery

I have a radio button in ASP.NET 我在ASP.NET中有一个单选按钮

 <asp:RadioButton ID="RadioButton2" Text="Yes" GroupName="rb" Checked="false" runat="server" />

I want to use bootstrap-switch that i got from HERE 我想使用从这里获得的引导开关

I simply want to have the "Switching" effect on my radio buttons, instead of the default circles offered by ASP.NET. 我只希望单选按钮具有“切换”效果,而不是ASP.NET提供的默认圆圈。

Here is what i did: 这是我所做的:
1) Added Radio Button on my aspx page. 1)在我的aspx页面上添加了单选按钮。 As shown above. 如上图所示。
2) used the following script 2)使用以下脚本

   <script type="text/javascript">

        $(document).ready(function () {

            $("input:radio").bootstrapSwitch();

          $("input:radio").on("click", function () {

                console.log("Clicked");
                $("input:radio").bootstrapSwitch('toggleState', true);
            });
        });


    </script>

Now the behaviour is as such that If I click on "OFF" option, it slides to "ON"... but if i click on "ON" option it doesn't move back to OFF. 现在的行为是这样的:如果我单击“关闭”选项,它会滑到“打开” ...但是如果我单击“打开”选项,它不会移回关闭。 Interestingly, If I click on the Text of radio Button which is "YES", it then toggles. 有趣的是,如果我单击“是”单选按钮的“文本”,则会进行切换。
Any help will be much appreciated. 任何帮助都感激不尽。

When you use $("input:radio").bootstrapSwitch(); 当您使用$("input:radio").bootstrapSwitch(); , it will work, you don't have to write separate code for handling click event. ,它将起作用,您无需编写单独的代码来处理click事件。 I have tried a Similar example . 我试过类似的例子

  $('input[type="radio"]').bootstrapSwitch(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="http://www.bootstrap-switch.org/docs/css/bootstrap.min.css" rel="stylesheet"> <link href="http://www.bootstrap-switch.org/dist/css/bootstrap3/bootstrap-switch.css" rel="stylesheet"> <script src="http://www.bootstrap-switch.org/docs/js/bootstrap.min.js"></script> <script src="http://www.bootstrap-switch.org/dist/js/bootstrap-switch.js"></script> <div class="col-sm-6"> <h3 class="h5">Example</h3> <input type="radio" name="radio2" checked data-radio-all-off="true" class="switch-radio2"> <input type="radio" name="radio2" data-radio-all-off="true" class="switch-radio2"> <input type="radio" name="radio2" data-radio-all-off="true" class="switch-radio2"> </div> 

And if you look into your code, you are always setting toggleState to true in $("input:radio").on("click"..... , where as you should first check if $(this).attr("checked") is true, then set it to true , else set toggleState to false. 并且,如果您查看代码,则总是在$("input:radio").on("click".....中将toggleState设置为true,在这里应首先检查$(this).attr("checked")为true,然后将其设置为true,否则将toggleState设置为false。

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

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