[英]Text shown after a user selects an item on a drop down menu?
我已经困扰了好几天了,无法解决这个问题。 我对此并不陌生,其他人也曾尝试帮助我,但没有成功。 我基本上是试图制作一个下拉菜单,当用户单击下拉列表中的某个项目时,下拉菜单旁边或下方会显示一个文本,显示“您已单击了此菜单”或我想要的任何内容,例如“选择此项,您已经节省了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>
还是有更好的方法呢? 谢谢你的时间。
试试这个-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>
将这些值保存在您的选项中。否则,您将不得不与全文进行比较。
<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>
您不必指定参数的数据类型
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事件中添加javascript onchange="showRelatedValue(this.value)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.