简体   繁体   English

用户在下拉菜单上选择一个项目后显示的文本?

[英]Text shown after a user selects an item on a drop down menu?

I've been messing with this for days now and can't figure this out. 我已经困扰了好几天了,无法解决这个问题。 I am new to this and others have tried helping me but its not working. 我对此并不陌生,其他人也曾尝试帮助我,但没有成功。 I am basically trying to make a Drop Down menu form where when a user clicks on an item on the drop down list, a text appears next to the drop down or under it saying "You have clicked this" or anything I want such as "You have saved 10% by choosing this!" 我基本上是试图制作一个下拉菜单,当用户单击下拉列表中的某个项目时,下拉菜单旁边或下方会显示一个文本,显示“您已单击了此菜单”或我想要的任何内容,例如“选择此项,您已经节省了10%!”

Can somebody help with this? 有人可以帮忙吗? My current code is something like this: 我当前的代码是这样的:

<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>

Or is there a better way of doing this? 还是有更好的方法呢? Thanks for your time. 谢谢你的时间。

Try this - DEMO 试试这个-DEMO

<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>​

Save the values in your options.. Otherwise you would have to compare with the complete text.. 将这些值保存在您的选项中。否则,您将不得不与全文进行比较。

<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>

You don't have to specify the data type of the parameter 您不必指定参数的数据类型

 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");
 }

Also no need of adding javascript in the onchange event onchange="showRelatedValue(this.value)" 另外,无需在onchange事件中添加javascript onchange="showRelatedValue(this.value)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM