简体   繁体   English

使用Jquery的Rails App:只允许选择一个复选框(haml)

[英]Rails App using Jquery: Only allowed to select one checkbox (haml)

I have a credit card option (which when the box is selected you can choose from either American Express, Mastercard, Discover, or Visa). 我有一个信用卡选项(选中此框后,您可以选择美国运通卡,万事达卡,发现卡或Visa卡)。 If you unclick it, it's hidden. 如果你取消它,它就是隐藏的。

And same goes for the check option: if you click it, it shows the address where to send the check to, if you unclick it disappears. 同样适用于检查选项:如果您单击它,它会显示发送支票的地址,如果您取消选中它会消失。

So the first two functions are working as intended. 所以前两个功能正在按预期工作。 Now I want to be able to make it so you CANNOT click both check and credit card. 现在我想能够做到这一点,所以你不能点击支票和信用卡。 What is wrong with the third function? 第三个功能有什么问题? A friend suggested I have too many divs in my HAML (see gist https://gist.github.com/2654244 for content). 一位朋友建议我的HAML中有太多div(请参阅gist https://gist.github.com/2654244了解内容)。

And yes I'm aware I should be using radio buttons :) Will change later. 是的我知道我应该使用单选按钮:)稍后会改变。

= content_for :javascripts do
  :javascript
      $(function(){
        $("#cc_checkbox").click(function(){
        if($(this).children("input").is(':checked')){
          $(this).next().show();
          }
        else{
        $(this).next().hide();
        }
      });
     })

     $(function(){
       $("#check_checkbox").click(function(){
       if($(this).children("input").is(':checked')){
     $(this).next().show();
     }
  else{
     $(this).next().hide();
     }
   });
 })

     $(function() {
       var $paymentSplit = $('input.paymentSplit');
         $paymentSplit.click(function() {
           $paymentSplit.removeAttr('checked');
           $(this).attr('checked', true);
         });
      });

Make them radio buttons instead of checkboxes. 使它们成为单选按钮而不是复选框。 Only one radio button of the same name can be selected at any time. 任何时候都只能选择一个同名的单选按钮。

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

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