因此,這裏是場景。我有2個下拉。第一個有onchange
函數來加載第二個下拉列表中的數據。它完全正常工作,但我想實現使用onchange
函數onload
加載第二個下拉列表中的數據。AJAX在頁面加載之前運行onchange事件
這裏是我的功能:
function fetch_select(val)
{
$.ajax({
type: 'post',
url: '../function/fetch_car_package.php',
data: {
get_option:val
},
success: function (response) {
document.getElementById("new_select").innerHTML=response;
}
});
}
這裏是我的下拉列表:
<select name='cItemID' onchange="fetch_select(this.value);">
<?php
$sql = "";
$sth = $dbh->prepare($sql);
$sth->execute();
$result = $sth->fetchAll();
foreach($result as $row) {
if ($_SESSION['cItemID'] == $row['Item ID']) {
$selected = "selected";
} else {
$selected = '';
}
echo "<option value='".$row['Item ID']."' $selected>".$row['Item Name']."</option>";
}
?>
</select>
我的AJAX頁面處理:
if(isset($_POST['get_option'])){
$ItemID = $_POST['get_option'];
$sql = "";
$sth = $dbh->prepare($sql);
$sth->execute();
$result = $sth->fetchAll();
foreach($result as $row) {
echo "<option value='".$row['cCLID']."' $selected>".$row['cDescription']."</option>";
}
exit;
}