[英]How to pass php array string to javascript function as a parameter?
I am iterating through a php array to display a list of photo filters. 我正在遍历php数组以显示照片滤镜列表。 I need to be able to pass the selected filter name to my javascript function to load it, for now I am only able to get the $i
id but the full filter name would be so much more easier. 我需要能够将选定的过滤器名称传递给我的javascript函数以加载它,目前,我只能获得$i
id,但是完整的过滤器名称将更加容易。
How can pass $filters[$i]
to getFilterName()
? 如何将$filters[$i]
传递给getFilterName()
?
<?php
for ($i = 0; $filters[$i]; $i++)
{
if (strpos($filters[$i], '.png'))
{
?>
<!-- get php variable for javascript : -->
<div id="filter-target" style="display: none;">
<?php
echo htmlspecialchars($i);
?>
</div>
<!-- ... -->
<?php
echo '<div tabindex="-1" class="filter_box" onclick="getFilterName('.$i.')">';
echo '<img class="filter" src="pictures/filters/'.$filters[$i].'"/>';
echo '</div>';
}
}
?>
Thank you! 谢谢!
Because the filename is a string, you need to escape it in ' '
like so: 由于文件名是一个字符串,因此您需要像这样在' '
对其进行转义:
<?php
for ($i = 0; $filters[$i]; $i++)
{
if (strpos($filters[$i], '.png'))
{
?>
<!-- get php variable for javascript : -->
<div id="filter-target" style="display: none;">
<?php
echo htmlspecialchars($i);
?>
</div>
<!-- ... -->
<?php
echo '<div tabindex="-1" class="filter_box" onclick="getFilterName(\''.$filters[$i].'\')">';
echo '<img class="filter" src="pictures/filters/'.$filters[$i].'"/>';
echo '</div>';
}
}
?>
Otherwise it would treat the string as a variable name in JavaScript and wouldnt fint it because it doesnt exist. 否则,它将在JavaScript中将字符串视为变量名,并且不会查找它,因为它不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.