简体   繁体   中英

Change DIV background-color with Coffescript

I am currently doing the password strength indicator on Ruby on Rail web application, and I was wonder why I cannot change the password indicator background-color even when the validate status is good or strong, it is still in red color.

if (validatePassword.status ? 'weak')
    $('#validateMeter').css('background-color','red')
else if (validatePassword.status ? 'good' || 'strong')
    $('#validateMeter').css('background-color','green')

Is there any solution? Thank. 在此处输入图片说明

I'm not entirely sure what should happen if the status is not defined (as you're checking for that) but the following should work:

color = if validatePassword.status in ['good', 'strong'] then 'green' else 'red'

$('#validateMeter').css('background-color', color)

The code above will set the color to green if the validatePassword.status is good or strong otherwise it will be set to red .

Should you want to unset the color entirely, I'd recommend trying something along the lines of this:

color = switch validatePassword.status
  when 'weak'           then 'red'
  when 'good', 'strong' then 'green'
  else ''

$('#validateMeter').css('background-color', color)

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