php数组和jquery添加类
我试图根据询问日期、开始时间和结束时间的表格突出显示可用的办公桌。我能够回显所有可用的办公桌,但我无法让 jquery 与其一起工作。
foreach ($allData as $desk => $id){
foreach ($id as $computer){?>
<div id="<?php echo $desk?>"></div><?php
}
}
<style>
.availableDesk{
background: #000000
}
</style>
<script>
$(document).ready(function() {
jQuery( " <?php echo $desk ?> " ), addClass('availableDesk') ;
})
</script>
办公桌:
<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>
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: #000000
}
</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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少 PHP 块末尾的
?>
。您的 jQuery 代码有一些问题。首先,如果
$desk
是 ID,则需要执行$('#ID')
。其次,在addClass
之前有一个逗号而不是句点。PS HTML ID 不应以数字开头。另外,你不能有多个具有相同 ID 的元素,我建议你使用类。
You're missing the
?>
at the end of your PHP block.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 beforeaddClass
instead of a period.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.
我可以在这里看到一些潜在的问题...
从语法上讲,我认为这应该是:
注意选择器中的 #,这告诉 jQuery 您正在寻找一个 id。 addClass 方法可用于返回的项目,因此您需要一个停止符 (.) 而不是逗号 (,)
另一个问题是您在 PHP 的 foreach 循环中写入 id - 我假设每个桌子都有一个唯一的 id - 当您编写
jQuery("#")
时,该语句位于 foreach 循环之外,因此它与您要定位的 id 不匹配。如果您知道该桌子可以用 PHP 编写,那么最好的选择是在编写桌子时设置类......
I can see a couple of potential issues here...
Syntactically, I think this should be:
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...