我已经搞砸了好几天了,无法解决这个问题。我是新手,其他人试图帮助我,但它不起作用。我基本上尝试制作一个下拉菜单表单,当用户点击下拉列表中的某个项目时,下拉或下面的文本会显示“您已点击此内容”或我想要的任何内容,例如“你选择这个已经节省了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>
或者有更好的方法吗?谢谢你的时间。
答案 0 :(得分:0)
试试这个 - 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>
答案 1 :(得分: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>
答案 2 :(得分: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事件onchange="showRelatedValue(this.value)"