我正在嘗試在選擇輸入中獲取所選選項的值,但它不起作用。當我改變選項時它應該顯示一個警告框,但沒有任何事情發生。這是我的代碼。獲取選擇輸入更改的值
<html>
<head>
<?PHP
$content ="";
$mammalArray = array();
$otherArray = array();
array_push($mammalArray,"dog");
array_push($mammalArray,"cat");
array_push($mammalArray,"pig");
array_push($otherArray,"bird");
array_push($otherArray,"fish");
$content .= "<select><option value='0'>Please Select a Pet Item</option>";
$content .= "<optgroup label='Mammals'>";
foreach($mammalArray as $animal=>$pet)
{
$content .= "<option>" . $pet . "</option>";
}
$content .= "</optgroup><optgroup label='Other'>";
foreach($otherArray as $animal=>$pet)
{
$content .= "<option>" . $pet . "</option>";
}
$content .= "</optgroup></select>";
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript">
$("select").change(function(){
// Do something here.
//alert("hello");
var option = $(this).find('option:selected').val();
alert(option);
});
</script>
</head>
<body>
<?php
echo($content);
?>
</body>
</html>
首先,值」;? –