簡體   English   中英

用戶在下拉菜單上選擇一個項目后顯示的文本?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM