我试图在我的 PHP 生成的下拉列表中获取所选项目的 id。现在控制台说“Uncaught TypeError: Object #exchanges option:selected has no method 'attr'”我不知道出了什么问题。
<html>
<head>
<title>Administration</title>
<style>
#exchanges {
width: 300px;
}
</style>
<script src="http://codeorigin.jquery.com/jquery-2.0.3.min.js"></script>
</head>
<body>
<?php populateDropDown(); ?>
<button id="display_button">Display Information</button>
</body>
<script>
$(document).ready(function(){
$('#display_button').on('click', function(){
var dropdown_id = ('#exchanges option:selected').attr('id');
alert(dropdown_id); // doesn't work
});
});
</script>
</html>
PHP函数:
function populateDropDown(){
$conn = connectPDO();
echo '<select id="exchanges">';
foreach($conn->query('SELECT * FROM exchange') as $row) {
echo '<option id ='.$row['exchangeID'].'>';
echo $row['exchange-name'];
echo '</option>';
}
echo '</select>';
}