简体   繁体   中英

apply a css class conditionally if another class exists jquery/rails

I have a form with checkboxes - if one of these is checked, there is some jquery that applies the css class checkboxed .

I have another div further down the page that is hidden by default - but if any of the form checkboxes are checked, it should become visible.

I'd like to use something like this

<div class="<%= "hide" unless checkboxed.exists? %>">

but obviously that doesn't work. Is there a way to test if a css class exists on a given page? I know I'll have to use jQuery, but how to then tie this in with the conditional CSS class in the hidden div ?

You need to add the div showing logic to the place where you react to checking a box.The addition might look like this:

if($('.checkboxed').length)
    $('#hiddenDiv').show();

Your hidden div must have an ID, here I assumed id="hiddenDiv" .

var checkbox = $('checkbox').attr("checked");

if (checkbox == 'checked') {
    $('#divID').show(); // #divID is the ID for the hidden DIV which you want to show.
}

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