0

I am wanting to show a hidden field but only when an option is selected in the dropdown. It does not matter which option is selected but I need one to be selected.

Javascript:

    <script type="text/javascript">
function Select(sel,id,nu){
document.getElementById(id).style.display=sel.selectedIndex==nu?'block':'none';
}
</script>

Selection:

    <select name="options[1]" id="select_1" 
class=" required-entry product-custom-option" 
title="" onchange="opConfig.reloadPrice();displayCondition();Select(this,divShow,1)">
<option value="" >-- Please Select --</option>
<option value="1"  price="0" >Perfect </option>
<option value="2"  price="-35" >Excellent </option>
<option value="3"  price="-105" >Good </option>
<option value="4"  price="-140" >Poor </option>
<option value="5"  price="-252" >Broken </option>
</select> 

The above onchange will only work if the first item is selected, so I am unsure how to make it work for any item selected. Also, I know the divShow should have some ' around it but I don't know how to write it into the following php:

    $extraParams .= ' onchange="opConfig.reloadPrice();displayCondition();Select(this,'."divShow".',1)"';

Div Box:

    <div id="divShow" style="display:none;">
<?php echo $this->getPriceHtml($_product) ?></div>
4

1 回答 1

0

您必须显示简单的引号才能divShow生成 JS 字符串。在 PHP(以及大多数常用语言)中,您可以使用以下命令对它们进行转义\

$extraParams .= ' onchange="opConfig.reloadPrice();displayCondition();Select(this,\'divShow\',1)"';

如果没有这些引号,您的 JS 将尝试查找divShow变量。

于 2013-03-30T00:22:42.220 回答