2015-11-06 70 views
3

只是把一個簡單的html網頁放在一起,我想添加一個下拉框,我會選擇一個選項,它會根據選擇的內容顯示文本。HTML下拉框重定向

我有這個到目前爲止。我知道我可能不得不使用JavaScript,但我不知道從哪裏開始。

<html> 
<head> 
<title> My webpage </title> 
</head> 
<body> 
<h1> Times Tables! </h1> 
<p> Click on the drop down box to select the tables you want to see </p> 


<select> 
<option value="Please select">Please select</option> 
<option value="1x0 =0 1x1 =1 1x2 =2 1x3 =3">1 time table</option> 
<option value="2 times table">2 times table</option> 
<option value="3 times table">3 times table</option> 
<option value="4 times table">4 times table</option> 
<option value="5 times table">5 times table</option> 
<option value="6 times table">6 times table</option> 
<option value="7 times table">7 times table</option> 
<option value="8 times table">8 times table</option> 
<option value="9 times table">9 times table</option> 
<option value="10 times table">10 times table</option> 
<option value="11 times table">11 times table</option> 
<option value="12 times table">12 times table</option> 

</select> 
<p> 
</p> 

</body> 
</html> 

感謝

回答

0

舉一個ID來選擇標籤。就像

<select id="selected"> 

然後jQuery代碼:

<script type="text/javascript"> 

    $(document).ready(function(){ 
     $('#selected').change(function(){ 
     alert($(this).val());               

     }); 
</script> 
+0

謝謝!這幫助了很多 –

1

如果我理解正確的UR的問題..你要求,如何讓所選擇的選項的文字。如果是這樣,給一個名稱和ID來<select>箱..

<select name='sel_name' id='sel_name' onchange='get_selected()'>

,然後使用JavaScript和調用該函數選擇框onchange ..

function get_selected() 
{ 
//this will get you the selected option's text  
document.getElementById('sel_name').options[document.getElementById('sel_name').selectedIndex].text; 

// to just get the value of the select box you can use 
document.getElementById('sel_name').value; 
} 
1

你說得對,你想使用JavaScript。事實上,使用jQuery對於事件處理來說會更好。

如果創建了一個功能 $('#selectID').on('change', function(){}); (你必須給你的選擇,並通過ID識別它) 然後函數括號內寫代碼,做你想做什麼。根據你的情況,如果是顯示某種形式的文字,你可以只
alert($('#selectID').val()) 或者只是jQuery來改變DOM