0

我已经搞砸了好几天了,无法弄清楚。我对此并不陌生,其他人也曾尝试帮助我,但它不起作用。我基本上是在尝试制作一个下拉菜单表单,当用户单击下拉列表中的某个项目时,下拉列表旁边或下方会出现一个文本,上面写着“您已单击此”或我想要的任何内容,例如“选择这个,您节省了 10%!”

有人可以帮忙吗?我当前的代码是这样的:

<form>
<p align="center"><b>Select a Payment:</b>
<select id="setit" onchange="javascript:showRelatedValue(this.value)">
<option value="">Select one</option>
    <option value="">1 Month: $4.99</option>
    <option value="">3 Months: $14.99</option>
    <option value="">6 Months: $29.99</option></select>
<br />
<br />
     <input type="button" value="Add to Cart"
onclick="window.open(setit.options[setit.selectedIndex].value)">
</p>
</form>
<script>
     function showRelatedValue(String value)
     {
         if(value==1 Month: $4.99)
         alert("You have selected One Month");
         if(value==3 Months: $14.99)
         alert("You have selected Three Months");
         if(value==6 Months: $29.99)
         alert("You have selected Six Months");
     }
</script>

或者有没有更好的方法来做到这一点?谢谢你的时间。

4

3 回答 3

0

试试这个 -演示

<form>
<p align="center"><b>Select a Payment:</b>
<select id="setit" onchange="javascript:showRelatedValue(this.value)">
<option value="">Select one</option>
    <option value="1">1 Month: $4.99</option>
    <option value="3">3 Months: $14.99</option>
    <option value="6">6 Months: $29.99</option></select>
<br />
<br />
     <input type="button" value="Add to Cart"
onclick="window.open(setit.options[setit.selectedIndex].value)">
</p>
</form>
<script>
     function showRelatedValue(value)
     {
         if(value==1)
         alert("You have selected One Month");
         if(value==3)
         alert("You have selected Three Months");
         if(value==6)
         alert("You have selected Six Months");
     }
</script>​
于 2012-10-24T23:54:49.710 回答
0

保存选项中的值。否则,您将不得不与完整的文本进行比较。

<form>
<p align="center"><b>Select a Payment:</b>
<select id="setit" onchange="javascript:showRelatedValue(this.value)">
<option value="">Select one</option>
    <option value="4.99">1 Month: $4.99</option>
    <option value="14.99">3 Months: $14.99</option>
    <option value="29.99">6 Months: $29.99</option></select>
<br />
<br />
     <input type="button" value="Add to Cart"
onclick="window.open(setit.options[setit.selectedIndex].value)">
</p>
</form>

<script>
     function showRelatedValue(value)
     {
         if(value== '4.99')
         alert("You have selected One Month");
         if(value== '14.99')
         alert("You have selected Three Months");
         if(value== '29.99')
         alert("You have selected Six Months");
     }
</script>
于 2012-10-24T23:55:35.763 回答
0

您不必指定参数的数据类型

 function showRelatedValue(value)
 {
     if(value == '1')
        alert("You have selected One Month");
     else if(value == '3')
        alert("You have selected Three Months");
     else if(value == '6')
        alert("You have selected Six Months");
 }

也不需要在 onchange 事件中添加 javascriptonchange="showRelatedValue(this.value)"

于 2012-10-24T23:55:56.903 回答