.addClass,基于值的IF语句

我正在尝试使用if语句和两个.addClass背景颜色变量,这些变量基于’shift_times’=“SAT AM / SUN PM”或=“SAT PM / SUN AM”的列值。 我正在寻找一个值是数字而不是文本的解决方案。

$query = "SELECT COUNT(confirmed) as cnt, position, shift_times FROM volConfirm WHERE confirmed='yes' GROUP BY shift_times, position order by position"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo ""; echo "" . $row['position'] ." ". $row['shift_times'] ."= " . $row['cnt'] . ""; echo ""; } 

这是你想要的东西吗?

 $query = "SELECT COUNT(confirmed) as cnt, position, shift_times FROM volConfirm WHERE confirmed='yes' GROUP BY shift_times, position ORDER BY position"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { if ($row['shift_times'] === 'SAT AM/SUN PM') { echo ''; } else if ($row['shift_times'] === 'SAT PM/SUN AM') { echo ''; } else { echo ''; } echo "" . $row['position'] ." ". $row['shift_times'] ."= " . $row['cnt'] . ""; echo ""; } 

我假设您正在寻找基于您的问题内容的jQuery答案以及addClass是一个jQuery函数的事实……如果我错了,我道歉。 如果这就是你的意思,这是你的答案:

PHP:

 while($row = mysql_fetch_array($result)) { echo ""; echo "" . $row['position'] ." ". $row['shift_times'] ."= " . $row['cnt'] . ""; echo ""; } 

jQuery的:

 $(function() { $.each($('.shifttimes'), function() { var $this = $(this); $this.text() == "SAT AM/SUN PM" ? $this.addClass('className') : $this.addClass('otherClassName'); }); });