简体   繁体   中英

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). 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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