php array and jquery add class
I am trying to get desks highlighted that are available based off of a form that asks for the the day, time start and time end. I am able to echo out all the desks that are available, but I cant get the jquery to work with it.
foreach ($allData as $desk => $id){
foreach ($id as $computer){?>
<div id="<?php echo $desk?>"></div><?php
}
}
<style>
.availableDesk{
background: #00000开发者_StackOverflow0
}
</style>
<script>
$(document).ready(function() {
jQuery( " <?php echo $desk ?> " ), addClass('availableDesk') ;
})
</script>
DESKS:
<ul class="tabs">
<li id="1A"><a href="#1A"><div id="ddesk"></div></a></li>
<li id="1B"><a href="#1B"><div id="ddesk"></div></a></li>
<li id="1C"><a href="#1C"><div id="ddesk"></div></a></li>
</ul>
You're missing the ?>
at the end of your PHP block.
<?php
foreach ($allData as $desk => $id){
foreach ($id as $computer){?>
<div id="<?php echo $desk?>"></div><?php
}
} // You need a "?>" here
?>
There are a few things wrong with your jQuery code. First off, if $desk
is an ID, you need to do $('#ID')
. Second, you have a comma before addClass
instead of a period.
jQuery("#<?php echo $desk ?>").addClass('availableDesk');
P.S. HTML ID's aren't supposed to start with a number. Also, you cannot have multiple elements with the same ID, I suggest you use classes instead.
I can see a couple of potential issues here...
jQuery( "<?php echo $desk ?>" ), addClass('availableDesk') ;
Syntactically, I think this should be:
jQuery("#<?php echo $desk ?>").addClass('availableDesk');
Note the # in the selector, this tells jQuery that you are looking for an id. The addClass method is available on the returned items, so you need a stop (.) not a comma (,)
The other gotcha is that you are writing the id in a foreach loop in PHP - I presume that each desk has a unique id - when you write jQuery("#<?php echo $desk ?>")
the statement is outside of the foreach loop, so it won't match the ids you are targeting.
If you know that the desk is available in PHP, the best option would be to set the class as you write the desk...
<div id="<?php echo $desk?>" class="availableDesk"></div>
精彩评论