[英]Pass and update php variable into javascript
Is there a way I can pass the php option value variable $option_value['price'] into javascript as a string, then each time a different option is chosen, the javascript 'updates' the string with the new value. 有没有一种方法可以将php选项值变量$ option_value ['price']作为字符串传递给javascript,然后每次选择不同的选项时,javascript都会使用新值“更新”字符串。 I have tried with onchange() but must be doing something wrong. 我尝试使用onchange(),但一定做错了。 I am sure my mistake is because php is server side and javascript is client side, but I have no idea how to get the two to act how I want. 我敢肯定,我的错误是因为php是服务器端,而javascript是客户端,但是我不知道如何让两者按照我的意愿行事。
<?php if ($options) { ?>
<?php foreach ($options as $option) { ?>
<?php if ($option['type'] == 'select') { ?>
<span id="option-<?php echo $option['product_option_id']; ?>" class="option">
<select name="option[<?php echo $option['product_option_id']; ?>]" onchange="getIt(this)">
<?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 $option_value['price']; ?>
<?php } ?>
</option>
<?php } ?>
</select>
</span>
I have tried: 我努力了:
<script type="text/javascript">
function getIt(elm) {
var val = elm.options[elm.selectedIndex].text;
window.alert(val);
}
</script>
and 和
<script type="text/javascript">
function getIt() {
var val = "<?php $option_value['price']; ?>";
window.alert(val);
}
</script>
<script type="text/javascript">
function getIt() {
var val = "<?php echo $option_value['price']; ?>";
window.alert(val);
}
</script>
Like so? 这样吗 Looks to me like you just forgot the "echo". 在我看来,就像您只是忘记了“回声”一样。
EDIT- 编辑-
<script type="text/javascript">
var val = "<?php echo $option_value['price']; ?>";
function getIt(elm) {
val = elm.options[elm.selectedIndex].text;
window.alert(val);
}
</script>
Okay, Revoking my previous entry. 好的,撤销我以前的条目。
What you need to do is to add selected
keyword to your <option>
element when you match your option_value['price']
您需要做的是当您匹配option_value['price']
时将selected
关键字添加到<option>
元素中
Now yuou need to arrange your data in a little bit different way. 现在,您需要以一些不同的方式来安排数据。 In your javascript, prepare an array that will map $option_value['product_option_value_id']
to $option_value['price'];
在您的JavaScript中,准备一个数组,将$option_value['product_option_value_id']
映射到$option_value['price'];
. 。 When you build your <select>
, for each $option_value['product_option_value_id']
check if its price matches the one that came in initially from php. 当您构建<select>
,对于每个$option_value['product_option_value_id']
请检查其价格是否与最初来自php的价格相匹配。
Your getIt will simply take the <option value="___">
value, find it in that map, and show it to the user using the alert()
window. 您的getIt只需获取<option value="___">
值,在该映射中找到它,然后使用alert()
窗口将其显示给用户。
Here is an example how you can do it. 这是一个示例,您可以如何做。 What you have to do are: 您要做的是:
fruits
array so it looks like the one in the example. 构建您的fruits
数组,使其看起来像示例中的数组。 I have prepared page that will show you this working here: http://jsfiddle.net/7uudp/ 我已经准备好了页面,可以在这里显示此工作: http : //jsfiddle.net/7uudp/
Below is what it contains. 以下是其中包含的内容。 Notice that this example does not use your variables. 请注意,此示例未使用您的变量。 You will need to use them the way you want/need. 您将需要以所需/需要的方式使用它们。 I have also defined entry_fruit
to show you how to chose the default one to be selected. 我还定义了entry_fruit
,向您展示如何选择默认的默认entry_fruit
。 Hope it will help. 希望它会有所帮助。
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
fruits = [{n:"apple",p:1},{"n":"strawbery",p:3}];
entry_fruit="strawbery";
function build_select(select,list,which)
{
$.each( list, function(i,v){
var selopt="" ;
if ( v.n == which ) selopt="selected" ;
$(select).append("<option "+selopt+" value="+i+">"+v.n+" - $"+v.p+"</option>");
}) ;
}
$(document).ready(function(){
build_select($("select[name=fruits]"),fruits,entry_fruit);
$("select[name=fruits]").change(function(){
alert("Price is $" + fruits[$(this).val()].p);
}) ;
})
</script>
</head>
<body>
<select name="fruits"></select>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.