2014-04-07 20 views
0

我填充的選擇下拉菜單中使用PHP如何從我的選擇人口稠密獲取文本值與PHP

<select name="course" onchange="tester()"> 
    <option>Select Course</option> 
     <?php $result= mysql_query('SELECT DISTINCT Course FROM tasks'); ?> 
     <?php while($row= mysql_fetch_assoc($result)) { ?> 
     <option value="<?php echo htmlspecialchars($row['Course']);?>"> 
      <?php echo htmlspecialchars($row['Course']); ?> 
     </option> 
     <?php } 
     ?> 
</select> 

MySQL數據上的變化由tester()功能打印

<script type="text/javascript" > 
    function tester() 
    { 
    var testMe = $('#course option:selected').text() 
    alert(testMe); 
    } 
</script> 

到我收到警報:

"The page at localhost says: undefined" 

回答

3

$('#course option:selected')未定義。

在你的HTML您有:

<select name="course" onchange="tester()"> 

如果你想使用#course選擇,你需要一個ID添加到HTML代碼。

即。

<select id="course" name="course" onchange="tester()"> 
0

確保腳本運行#course option:selected被定義。

將代碼包裝在.ready調用中是一種很好的做法。這可確保所有(靜態)HTML已加載。

相關問題