0
我試圖在我生成的PHP下拉菜單中獲取所選項目的ID。現在控制檯說:「Uncaught TypeError:Object #exchanges option:selected has no method'attr'」我無法弄清楚發生了什麼問題。無法獲取下拉項目的ID
<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>';
}
選擇菜單本身可以使用該方法,但其子項(選項)不能。 默認行爲是爲該表單元素返回IT選定值,而不是其子標識。 – Dawson