将动态变量传递给Javascript函数?
<?php if ($option['type'] == 'select') { ?>
<div id="option-<?php echo $option['product_option_id']; ?>" class="option">
<?php if ($option['required']) { ?>
<span class="required">*</span>
<?php } ?>
<b><?php echo $option['name']; ?>:</b><br />
<select name="option[<?php echo $option['product_option_id']; ?>]">
<!-- <option value=""><?php echo $text_select; ?></option> -->
<?php foreach ($option['option_value'] as $option_value) { ?>
<option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
(<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
<?php } ?>
</option>
<?php } ?>
</select>
<?php if ($option_value['image'] != 'image/' && $option_value['image'] != 'image/no_image.jpg' ) { ?><?php
list($simgwidth) = getimagesize($option_value['image']);
$selectimgpos = $simgwidth + 35;
?>
<center>
<span id="img<?php echo $option['product_option_id']; ?>" style="position: absolute; display: none; left: -<?php echo $selectimgpos; ?>px; background: #eee; padding: 10px; border: 1px dotted #666; z-index: 90;">
<img src="<?php echo $option_value['image']; ?>">
<br /><strong><?php echo $option['name']; ?></strong>
</span></center><?php } ?>
</div>
<br />
<?php } ?>
您好,我是编程新手,我需要有关上述代码部分的帮助。通常,此代码仅显示一个带有选项的选择下拉框。
我通过在跨度内的底部添加额外的图像来修改它。我还计算了宽度并根据其宽度重新定位图像。
接下来我想做的是有一个 javascript,如果用户将鼠标悬停在选择框上或 onFocus 将显示跨度内的图像。如果他们更改选择框,图像将根据他们的选择而变化。我正在考虑模糊之后隐藏图片。
我找到了一些 javascript 来执行此操作,但我需要从 php.ini 传递动态变量。
我找到了这个 javascript,但代码不允许我传递变量。
<script type="text/javascript">
function swapImage(){
var image = document.getElementById("imageToSwap");
var dropd = document.getElementById("dd");
image.src ="images/"+dropd.value+".jpg";
};
</script>
由于 php 生成的 ID 不断变化,我无法使用静态变量。
任何帮助将不胜感激。谢谢大家。
PS: $option_value['image'] 是一个 php 数组。如何显示该数组中的第一个或最后一个元素?通常它类似于 array_[n] 但这个已经有括号了? $option_value['图像[0]'] ??
<?php if ($option['type'] == 'select') { ?>
<div id="option-<?php echo $option['product_option_id']; ?>" class="option">
<?php if ($option['required']) { ?>
<span class="required">*</span>
<?php } ?>
<b><?php echo $option['name']; ?>:</b><br />
<select name="option[<?php echo $option['product_option_id']; ?>]">
<!-- <option value=""><?php echo $text_select; ?></option> -->
<?php foreach ($option['option_value'] as $option_value) { ?>
<option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
(<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
<?php } ?>
</option>
<?php } ?>
</select>
<?php if ($option_value['image'] != 'image/' && $option_value['image'] != 'image/no_image.jpg' ) { ?><?php
list($simgwidth) = getimagesize($option_value['image']);
$selectimgpos = $simgwidth + 35;
?>
<center>
<span id="img<?php echo $option['product_option_id']; ?>" style="position: absolute; display: none; left: -<?php echo $selectimgpos; ?>px; background: #eee; padding: 10px; border: 1px dotted #666; z-index: 90;">
<img src="<?php echo $option_value['image']; ?>">
<br /><strong><?php echo $option['name']; ?></strong>
</span></center><?php } ?>
</div>
<br />
<?php } ?>
Hi, I am new to programming and I need help with the above section of code. Normally this code just displays a Select dropdown box with options.
I've modified it by adding an extra image at the bottom inside a span. I've also calculated the width and repositioned the image based on its -width.
What I'd like to do next is have a javascript where if the user hovers or onFocus on the Select box will show the image inside the span. And if they change the select box, the image will change based on what they selected. I'm thinking onBlur to hide the pic after.
I've found some javascripts to do this but I need to pass dynamic variables from the php.
I found this javascript, but the code doesn't let me pass variables.
<script type="text/javascript">
function swapImage(){
var image = document.getElementById("imageToSwap");
var dropd = document.getElementById("dd");
image.src ="images/"+dropd.value+".jpg";
};
</script>
Due to constantly changing IDs generated by php I can't be using static variables.
Any help would be appreciated. Thanks all.
PS: $option_value['image'] is a php array. How can I display the first or last element in this array? Normally it's something like array_[n] but this one already has brackets? $option_value['image[0]'] ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要将 php 变量传递给 javascript,您只需在正确的位置回显它们:
至于您的 PS:
PHP 数组可以是多维的,因此对于以下数组:
您可以访问项目:
您可以使用字符串作为键,因此您还可以命名您的键并使用字符串访问它们:
您可以通过以下方式访问这些项目:
To pass php variable to javascript, you just need to echo them out in the right place:
As for your PS:
PHP arrays can be multidimensional, so for the following array:
You can access the items as:
And you can use strings as keys, so you could also name your keys and access them with strings:
You can access the items as: