0

有没有办法可以将 php 选项值变量 $option_value['price'] 作为字符串传递给 javascript,然后每次选择不同的选项时,javascript 都会使用新值“更新”字符串。我试过 onchange() 但一定做错了什么。我确定我的错误是因为 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>

我努力了:

<script type="text/javascript"> 
function getIt(elm) {
var val = elm.options[elm.selectedIndex].text;
window.alert(val);
}
</script>

<script type="text/javascript">
function getIt() {
var val = "<?php $option_value['price']; ?>";
window.alert(val);
}
</script>
4

3 回答 3

1

Here is an example how you can do it. What you have to do are:

  1. Analyze the code (notice that I use jquery from google's account. You will need to at some point install jquery on your web site and use this one instead of the google's one.
  2. Build your fruits array so it looks like the one in the example.

I have prepared page that will show you this working here: 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. 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>
于 2012-08-30T21:09:48.510 回答
1
<script type="text/javascript">
function getIt() {
var val = "<?php echo $option_value['price']; ?>";
window.alert(val);
}
</script>

像这样?在我看来,您只是忘记了“回声”。

编辑-

<script type="text/javascript">
var val = "<?php echo $option_value['price']; ?>"; 
function getIt(elm) {
    val = elm.options[elm.selectedIndex].text;
    window.alert(val);
}
</script>
于 2012-08-30T03:09:33.517 回答
1

好的,撤销我之前的条目。

您需要做的是在匹配您的元素时selected向您的元素添加关键字<option>option_value['price']

现在你需要以不同的方式排列你的数据。在您的 javascript 中,准备一个将映射$option_value['product_option_value_id']$option_value['price'];. 当您构建您的, 时<select>$option_value['product_option_value_id']请检查其价格是否与最初从 php.ini 提供的价格相匹配。

您的 getIt 将简单地获取该值,在该地图中找到它,并使用窗口<option value="___">将其显示给用户。alert()

于 2012-08-30T03:14:32.360 回答